简体   繁体   中英

Python how to share variable between class methods, and what is the difference between instance attributes and class attributes

For example:

class MyClass:

    def __init__(self,Attr1):
        self.Attr1 = Attr1

    def method1(self):
        self.var1 = [some codes involves Attr1]

    def method2(self):
        self.var2 = [some codes involves var1 from method1]

Is it workable? But the variables in functions are local to the function. I don't know if I can call var1 from method1 .

The other way I can think of is to have self.var1 to be set in __init__ so it's a class attribute so that I can call it in method2 . But then I don't want to have a very large __init__ .

I also read something about a class attributes and an instance attributes — what's the difference between the two?

Instance Attributes

The variables that start with self. at the class belong to a specific singular class instance (not to a specific method), so it is very workable.

You can initialize them in class constructor:

class A:
    def __init__(self):
        self.instance_attribute = 'instance attribute'

And you can change their value for each instance separately:

a1 = A()
a2 = A()

a2.instance_attribute = 'sabich'

# 'instance attribute' (unchanged)
print(a1.instance_attribute)

# 'sabich'
print(a2.instance_attribute)

Class Attributes

class A:
    a = "class attribute"

Class attributes are common to all class instances and owned by the class.

They can be accessed via class name:

# 'class attribute'
print(A.a)

Or by specific instance:

a1 = A()

# 'class attribute'
print(a1.a)

You are using self. already, so the scope of your var1 is the class not the method.

Hence, you can use var1 in any other class method if you refer to it as self.var1 and if method1 is called first.

Is it workable?

Yup.

But the variables in functions are local to the function.

Nope, they are instance attributes, that is, they are attributes of certain instances of the class.

I don't know if I can call var1 from method1

You can, but you will need to create an instance and then call the method for that instance.

The other way I can think of is to have self.var1 to be set in __init__ so it's a class attribute so that I can call it in method2

It's better to set it in the constructor, because then you will not have to worry about method1 being called or not. But you can use it in method2 without a problem.

But then I don't want to have a very large __init__

You can create helper methods and call them.

I also read something about a class attributes and an instance attributes — what's the difference between the two?

Class attributes are attributes of classes, that is, they are attributes of the whole set of instances. Instance attributes are instances of attributes. Think about stackoverflow.com users. Each user has a username, that's instance-level. However, the number of users is an attribute of the users as a whole set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM