0
class demo():
   c_v=[]
   def __init__(self):
       demo.c_v.append('one')

class demo():
   c_v=[]
   def __init__(self):
       self.c_v.append('one')

Both yield same result? What are the usecases of both?

Shashi Tunga
  • 496
  • 1
  • 6
  • 24

1 Answers1

1

class variable will be available to all that creates instance from that class , just like the definition of methods within a class , while instance variable will be available to that instance only.

take this example :

class Example:
   class_i = [1] 
   def __init__(self):
        self.instance_i = [1]

a = Example()
b = Example()
a.class_i.append(2)
b.class_i.append(3)
a.instance_i.append(40)
b.instance_i.append(50)

print a.class_i
print b.class_i
print a.instance_i
print b.instance_i

will give you this output:

[1,2,3]
[1,2,3]
[1,40]
[1,50]
ddor254
  • 1,570
  • 1
  • 12
  • 28