Python Class Instance Variables
class AttributeInstanceClass(): def __init__(self): self.x = 6 myClass = AttributeInstanceClass() print(myClass.x)
Class instance variables are specific to the instance of the class. ie. The value of the instance variable is not shared among all instances of the class (see class variables):
class MyClass(): def __init__(self): self.x = 6 objectA = MyClass() objectB = MyClass() objectB.x = 13 print(objectA.x) print(objectB.x)
If you try to reference an instance variable as if it were a class variable, Python will tell you:
File "test.py", line 53, in <module> print(MyClass.x) AttributeError: type object 'MyClass' has no attribute 'x'
What Python calls an "instance variable" or "instance attribute" is often called a "field" in other languages.