I am very new to programming and started learning python. Might look very stupid question, so please pardon my ignorance. Consider the following snippet of code :
class Test1:
bar = 10
def display(self,foo):
self.foo=foo
print "foo : ",self.foo #80
def display1(self):
print "bar: ", self.bar #10
print "again foo: ", self.foo #80
if __name__ == '__main__':
test1 = Test1()
test1.display(80)
test1.display1()
print test1.bar #10
print test1.foo #80
I want to understand what is the difference between using foo and bar (wrt to where we have defined them) as in scope wise they are equally accessible at all places compared to each other and only difference is that one is inside function and other is inside Class but they both are still "instance" variable. So which is good practice?
Also, if I slightly modify display function as below :
def display(self,foo):
self.foo=foo
foo = foo
print "self.foo : ",self.foo
print "foo : ",foo
Can someone please explain how python sees this, as in what difference/significance this self
keyword is bringing in between two foo
.
Method 1: To get the list of all the attributes, methods along with some inherited magic methods of a class, we use a built-in called dir() . Method 2: Another way of finding a list of attributes is by using the module inspect .
Class attributes are the variables defined directly in the class that are shared by all objects of the class. Instance attributes are attributes or properties attached to an instance of a class. Instance attributes are defined in the constructor.
Use dot notation or setattr() function to set the value of class attribute. Python is a dynamic language. Therefore, you can assign a class variable to a class at runtime. Python stores class variables in the __dict__ attribute.
The __dict__ attribute will return a dictionary object of module attributes, functions and other definitions and their respective values. dir() is a built-in function that also returns the list of all attributes and functions in a module. Learn more about module attributes in Python Docs.
bar
is a class attribute and foo
is an instance attribute. The main difference is that bar
will be available to all class instances while foo
will be available to an instance only if you call display on that instance
>>> ins1 = Test1()
ins1.bar
works fine because it is a class attribute and is shared by all instances.
>>> ins1.bar
10
But you can't access foo directly here as it is not defined yet:
>>> ins1.foo
Traceback (most recent call last):
File "<ipython-input-62-9495b4da308f>", line 1, in <module>
ins1.foo
AttributeError: Test1 instance has no attribute 'foo'
>>> ins1.display(12)
foo : 12
>>> ins1.foo
12
If you want to initialize some instance attributes when the instance is created then place them inside the __init__
method.
class A(object):
bar = 10
def __init__(self, foo):
self.foo = foo #this gets initialized when the instance is created
def func(self, x):
self.spam = x #this will be available only when you call func() on the instance
...
>>> a = A(10)
>>> a.bar
10
>>> a.foo
10
>>> a.spam
Traceback (most recent call last):
File "<ipython-input-85-3b4ed07da1b4>", line 1, in <module>
a.spam
AttributeError: 'A' object has no attribute 'spam'
>>> a.func(2)
>>> a.spam
2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With