Reputation:
I'm currently watching Bucky's Python Programming tutorials. He has explained the concept of child classes and parent classes, like so:
class Dad:
var1 = "Hey I'm Dad"
class Mum:
var2 = "Hey I'm Mum"
class Child(Mum, Dad):
var3 = "Hey I'm a Child"
This, I understand completely. However, he then turns the classes into an object:
childObject = Child()
dadObject = Dad()
Why would he bother doing this, if he can just call Child.var3
to get the same result as childObject.var3
?
Upvotes: 1
Views: 3330
Reputation: 113978
a better example is
class Person:
name="Person"
def speak(self):
print "Hi! Im %s"%slf.name
class Dad(Person):
name = "Dad"
class Mom(Person):
name = "Mom"
class Child(Person):
name = "a Child"
age = 5
def speak(self):
print "Hewwo, I am a %d year old child!"%self.age
d = Dad()
m = Mom()
c = Child()
c.speak()
m.speak()
d.speak()
as to the difference (your question of static class access versus instance access)
class Child:
var3 = "whatever"
c = Child()
c.var3 = "something_else"
print c.var3
print Child.var3
Upvotes: 4