Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python class instance variables and class variables

I'm having a problem understanding how class / instance variables work in Python. I don't understand why when I try this code the list variable seems to be a class variable

class testClass():     list = []     def __init__(self):         self.list.append('thing')  p = testClass() print p.list  f = testClass() print f.list 

Output:

['thing'] ['thing', 'thing'] 

and when I do this it seems to be an instance variable

class testClass():     def __init__(self):         self.list = []         self.list.append('thing')  p = testClass() print p.list  f = testClass() print f.list 

Output:

['thing'] ['thing'] 
like image 657
jonathan topf Avatar asked Jan 02 '12 13:01

jonathan topf


People also ask

What is difference between class variable and instance variable in Python?

A class variable is a variable that defines a particular property or attribute for a class. An instance variable is a variable whose value is specified to the Instance and shared among different instances. We can share these variables between class and its subclasses. We cannot share these variables between classes.

What is difference between class variables and instance variables?

Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.

What are class variables in Python?

Class variable − A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.

What are the instance variables in Python?

What is an Instance Variable in Python? If the value of a variable varies from object to object, then such variables are called instance variables. For every object, a separate copy of the instance variable will be created. Instance variables are not shared by objects.


1 Answers

This is because of the way Python resolves names with the .. When you write self.list the Python runtime tries to resolve the list name first by looking for it in the instance object, and if it is not found there, then in the class instance.

Let's look into it step by step

self.list.append(1) 
  1. Is there a list name into the object self?
    • Yes: Use it! Finish.
    • No: Go to 2.
  2. Is there a list name into the class instance of object self?
    • Yes: Use it! Finish
    • No: Error!

But when you bind a name things are different:

self.list = [] 
  1. Is there a list name into the object self?
    • Yes: Overwrite it!
    • No: Bind it!

So, that is always an instance variable.

Your first example creates a list into the class instance, as this is the active scope at the time (no self anywhere). But your second example creates a list explicitly in the scope of self.

More interesting would be the example:

class testClass():     list = ['foo']     def __init__(self):         self.list = []         self.list.append('thing')  x = testClass() print x.list print testClass.list del x.list print x.list 

That will print:

['thing'] ['foo'] ['foo'] 

The moment you delete the instance name the class name is visible through the self reference.

like image 154
rodrigo Avatar answered Sep 18 '22 12:09

rodrigo