pythonoop:init\uuuuu()括号中的属性与括号外的属性之间的区别
我在这里和Python教程中读了很多关于Python OOP的内容。但有些问题困扰着我关于类属性的问题pythonoop:init\uuuuu()括号中的属性与括号外的属性之间的区别,python,oop,class,attributes,Python,Oop,Class,Attributes,我在这里和Python教程中读了很多关于Python OOP的内容。但有些问题困扰着我关于类属性的问题 class Demo: def __init__(self, name): # This attribute is linked to every instance through self parameter, # which contains the reference to the current instance. self.n
class Demo:
def __init__(self, name):
# This attribute is linked to every instance through self parameter,
# which contains the reference to the current instance.
self.name = name
demo1 = Demo("demo1")
demo2 = Demo("demo2")
demo1.attr = "new Attr" # Creating Attribute on the fly.
demo2.diffAttr = "diff Attr"
例如:
class Par(object):
def __init__(self, chip, fig):
self.fruit = chip
self.fig = fig
self.pear =10
self.chip=chip不应该吗
水果
没有出现在\uuuu init\uuuu()
括号中的属性列表中,但它被使用了李>
self.fruit=chip
和self.chip=chip
之间有什么区别\uuuu init\uuuuu()
中声明属性与在\uuuuu init\uuuuuuu()
外部声明属性之间有什么区别,例如self.pear=10
芯片
存储在名为水果
的属性中,而在第二种情况下,它的芯片
。类的\uuu dict\uuu
属性中的一个条目作为名称值对LHS
和RHS
创建。LHS表示用于链接到类的\uuu dict\uuu
属性中的值的属性的名称。你可以在那里有任何名字
为什么水果没有出现在init的属性列表中()
括号,但它被使用
原因和我上面写的一样<代码>水果
是类属性。您可以在\uuuu init\uuuu
方法中声明它,并将传递的值分配给该属性
在init()中声明属性有什么区别
并在_init()之外声明它,例如“self.pear=10”
\uuuu init\uuuu
中声明的变量是类属性。它们对于为该类创建的所有实例都是通用的。然而,您在外部声明的属性是特定于实例的。因此,具体来说,一个类的每个实例都可以有不同的属性集
class Demo:
def __init__(self, name):
# This attribute is linked to every instance through self parameter,
# which contains the reference to the current instance.
self.name = name
demo1 = Demo("demo1")
demo2 = Demo("demo2")
demo1.attr = "new Attr" # Creating Attribute on the fly.
demo2.diffAttr = "diff Attr"
因此,在上述代码中:-
- 实例
demo1
有两个属性,name
和attr
- 实例
demo2
也有两个属性,name
和diffAttr
,两个实例的第二个属性都不同
任何一个都是正确的
在python中,您不必声明变量self.fruit=chip
创建变量self.fruit
,然后将chip
变量的值分配给它
self.fruit=chip
存储chip
的值是self.fruit
变量,而self.chip=chip
将chip
的值存储在self.chip
<> >代码>芯片和<代码>标志< /代码>不属于PAR对象。它们只是用来访问调用函数时传递给函数的值
self
关键字告诉解释器它后面的变量是对象的成员。
在您的示例中,chip
和fig
的值将在执行函数后丢失,但self.fruit
、self.fig
和self.pear
的值将一直保留到对象离开为止。这意味着它们有不同的范围
执行\uuuu init\uuuu
函数后,您可以读取和修改self.fruit
、self.fig
和self.pear
的值,如下所示:
par = Par(oneChip, oneFig)
print par.fruit
print par.fig
par.pear = 20
print par.pear
self
将是Par
的一个实例self.\uuu dict\uuu
是一个保存self
属性的dict。当你说
self.fruit = chip
def __init__(self, chip, fig):
将在dict中创建一个条目:self.\uuu dict\uuuu
将包括{'fruit':chip}
当你说
self.fruit = chip
def __init__(self, chip, fig):
您声明必须向Par
传递两个值,chip
和fig
。(将为您将self
实例传递到\uuuu init\uuuu
)
因此,在\uuu init\uuu
内部,局部变量(不是属性!)芯片和图是已知的
self.fruit=chip
正在使用值chip
创建一个属性fruit
比如说,
class Par(object):
def __init__(self, chip, fig):
self.fruit = chip
self.fig = fig
self.pear = 10
# 99 is being passed in as the value of `chip`
# 'bar' is being passed in as the value of `fig`
par = Par(99,'bar')
print(par.__dict__)
屈服
{'fruit': 99, 'pear': 10, 'fig': 'bar'}
请注意,要访问通常使用的属性,例如,par.fruit
,而不是par.\uuu dict\uuu['fruit']
。我在上面展示par.\uuuuu dict\uuuuu
只是为了让您了解幕后的情况。您不应该在一个问题中问多个问题,因此括号内的变量是函数的参数,而不是属性。提出的问题表明缺乏超越OOP方面的理解。应该做更多的研究,提出更具体的问题来阐明特定的问题。当一种语言从静态类型转变为动态类型时,会有很多奇迹。谢谢你的清楚解释。