問題描述
我是 python 的新手,這些天我正在探索課程.我有一個關于類內的屬性和變量的問題:在類的主體中僅通過 q=1
定義屬性和通過定義 self.q=1定義屬性有什么區別
__init__
中的代碼>?例如,以下兩種可能性有什么區別?
I'm quite new in python and during these days I'm exploring classes. I have a question concerning attributes and variables inside classes: What is the difference between defining an attribute via just q=1
in the body of the class and via defining self.q=1
inside the __init__
? For example, what is the difference between the following two possibilities?
class MyClass1:
q=1
def __init__(self,p):
self.p=p
def AddSomething(self,x):
self.q = self.q+x
和
class MyClass2:
def __init__(self,p):
self.q=1
self.p=p
def AddSomething(self,x):
self.q = self.q+x
例如的輸出:
>>> my=MyClass1(2)
>>> my.p
2
>>> my.q
1
>>> my.AddSomething(7)
>>> my.q
8
不取決于是使用 MyClass1
還是 MyClass2
.MyClass1
和 MyClass2
都不會發生錯誤.
does not depend on whether MyClass1
or MyClass2
is used. Neither in MyClass1
nor in MyClass2
does an error occur.
推薦答案
python中的類及其實例使用類似字典的數據結構來存儲信息.
Classes as well as their instances in python uses dictionary like data structure to store the information.
因此,對于每個類定義,將分配一個字典來存儲類級別信息(類變量).對于該特定類的每個實例,將分配一個單獨的字典(self),其中將存儲實例特定信息(實例變量).
So for each class definition, a dictionary will be allocated where the class level information (class variables) will be stored. And for each instance of that particular class, a separate dictionary(self) will be allocated where the instance specific information(instance variables) will be stored.
所以現在下一個問題是:如何查找特定名稱??
So now the next question is: How the lookup for a particular name will be performed ??
這個問題的答案是,如果您通過某個實例訪問名稱,將首先搜索特定于實例的字典,如果在那里找不到該名稱,則將在類字典中搜索該名稱.因此,如果在兩個級別都定義了相同的值,則前一個將被覆蓋.
And answer to this question is that if you are accessing the names through some instance, the instance specific dictionary will be searched first and if the name is not found there, then the class dictionary will be searched for that name. So if the same value is defined at both levels that former one will be overridden.
請注意,當您編寫 d['key'] = val 時,其中 d 是字典,如果字典中不存在 'key',則會自動添加到該字典中.否則當前值將被覆蓋.在閱讀進一步說明之前,請記住這一點.
現在讓我們看看你用來描述問題的代碼:
Now lets go through the code you have used to describe your problem:
MyClass1
class MyClass1:
q=1
def __init__(self,p):
self.p=p
def AddSomething(self,x):
self.q = self.q+x
<小時>
1. my = Myclass1(2) #create new instance and add variables to it.
MyClass = {"q" : 1}
my = {"p" : 2}
<小時>
2. my.p # =2, p will be taken from Dictionary of my-instance.
<小時>
3. my.q # =1, q will be takn from MyClass dict. (Not present in dictionary of my-instance).
<小時>
4. my.AddSomething(7) # This method access the value of q (using self.q) first
# which is not defined in my dict and hence will be taken
# from dictionary of MyClass. After the addition operation,
# the sum is being stored in self.q. Note that now we are
# adding the name q to Dictionary of my-instance and hence
# a new memory space will be created in Dictionary of my-instance
# and the future references to self.q will fetch the value
# of self.q from dictionary of my-instance.
MyClass = {"q" : 1}
my = {"p" : 2, "q" : 8}
<小時>
5. my.q # =8, q now is available in dictionary of my-instance.
這篇關于Python 類屬性及其初始化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!