Class attributes vs Instance attributes
Class attributes are attributes which are owned by the class itself . They will be shared by all the instances of the class. Therefore they have the same value for every instance . We define class attributes outside of all the methods, usually they are placed at the top, right below the class header. On the other hand, instance attributes are owned by the specific instances of a class. This means for two different instances the instance attributes are usually different. Instance variables are defined inside a method, normally __new__ or __init__ , and they are local to that instance. The difference is that the attribute on the class is shared by all instances. The attribute on an instance is unique to that instance.Class Attributes
class a:
list=[]
y=a()
x=a()
x.list.append('a')
y.list.append('b')
x.list.append('c')
y.list.append('d')
print (x.list)
print (y.list)
output
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
Instance Attributes
class a:
def __init__(self):
self.list = []
y=a()
x=a()
x.list.append('a')
y.list.append('b')
x.list.append('c')
y.list.append('d')
print (x.list)
print (y.list)
output
['a', 'c']
['b', 'd']
In the examples above you can see, declaring the variables inside the class declaration makes them class members and not instance members. Declaring them inside the __init__ method makes sure that a new instance of the members is created alongside every new instance of the object.
Related Topics
- Python Interview Questions (Part 2)
- Python Interview Questions (Part 3)
- What is python used for?
- Is Python interpreted, or compiled, or both?
- Explain how python is interpreted
- How do I install pip on Windows?
- How do you protect Python source code?
- What are the disadvantages of the Python?
- How would you achieve web scraping in Python?
- How to Python Script executable on Unix
- What is the difference between .py and .pyc files?
- What is __init__.py used for in Python?
- What does __name__=='__main__' in Python mean?
- What is docstring in Python?
- What is the difference between runtime and compile time?
- How to use *args and **kwargs in Python
- Purpose of "/" and "//" operator in python?
- What is the purpose pass statement in python?
- Why isn't there a switch or case statement in Python?
- How does the ternary operator work in Python?
- What is the purpose of "self" in Python
- How do you debug a program in Python?
- What are literals in python?
- What is Python's parameter passing mechanism?
- What is the process of compilation and Loading in python?
- Global and Local Variables in Python
- Is there a tool to help find bugs or perform static analysis?
- What does the 'yield' keyword do in Python?
- Comparison Operators != is not equal to in Python
- What is the difference between 'is' and '==' in python
- What is the difference between = and == in Python?
- How are the functions help() and dir() different?
- What is the python keyword "with" used for?
- Is all the memory freed when Python exits?
- Difference between Mutable and Immutable in Python
- Explain split() methods of "re" module in Python
- Accessor and Mutator methods in Python
- How to Implement an 'enum' in Python
- Important characteristics of Python Objects
- How to determine the type of instance and inheritance in Python
- How would you implement inheritance in Python?
- How is Inheritance and Overriding methods are related?
- How can you create a copy of an object in Python?
- Static class variables in Python
- Difference between @staticmethod and @classmethod in Python
- How to Get a List of Class Attributes in Python
- Does Python supports interfaces like in Java or C#?
- What is used to create Unicode string in Python?
- Difference between lists and tuples in Python?
- What are differences between List and Dictionary in Python
- Different file processing modes supported by Python
- How do you append to a file in Python?
- What are the differences between the threading and multiprocessing?
- Is there any way to kill a Thread in Python?
- What is the use of lambda in Python?
- What is map, filter and reduce in python?
- Is monkey patching considered good programming practice?
- What is "typeerror: 'module' object is not callable"
- Python: TypeError: unhashable type: 'list'
- How to convert bytes to string in Python?
- What are metaclasses in Python?