Python 类属性及其初始化 [英] Python class attributes and their initialization

查看:17
本文介绍了Python 类属性及其初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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.MyClass1MyClass2 都不会发生错误.

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 类属性及其初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆