Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythonoop:init\uuuuu()括号中的属性与括号外的属性之间的区别_Python_Oop_Class_Attributes - Fatal编程技术网

pythonoop:init\uuuuu()括号中的属性与括号外的属性之间的区别

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

我在这里和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.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.fruit=chip
    self.chip=chip
    不应该吗
  • 为什么
    水果
    没有出现在
    \uuuu init\uuuu()
    括号中的属性列表中,但它被使用了
  • self.fruit=chip
    self.chip=chip
    之间有什么区别
  • \uuuu init\uuuuu()
    中声明属性与在
    \uuuuu init\uuuuuuu()
    外部声明属性之间有什么区别,例如
    self.pear=10
  • “self.fruit=chip”和“self.chip”有什么区别= “芯片”

    只有一个区别。在第一种情况下,您将
    芯片
    存储在名为
    水果
    的属性中,而在第二种情况下,它的
    芯片
    。类的
    \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方面的理解。应该做更多的研究,提出更具体的问题来阐明特定的问题。当一种语言从静态类型转变为动态类型时,会有很多奇迹。谢谢你的清楚解释。