class MyClass(object):
s = 'this is s sans function'
print(s)
def __init__(self):
self.s = 'this s is inside function'
print(self.s)
this is s sans function # this prints without an object. Would that make
# it a class variable, since it is called without an object?
y = MyClass()
this s is inside function # what is interesting here is that I didn't have
# to call the object. Just creating the object initiated the class function.
# So I really don't know what is happening here. But it is interesting....
The s is going to be a class variable (attribute), and you should be able to get to it like this:
MyClass.s
or
y = MyClass()
y.s
But, that attribute should be shared among all the classes, so what you’re doing by using self.s is only accessing the s in your object. Try this (off the top of my head):
print(y.s, MyClass.s)
They should be different, but let me know if they’re not and I can come up with a different demo.
Yes see the difference? When it is on the class, then that should be available to all of the objects. When it’s on an individual object it should only be on that one object. Now the next thing is you really shouldn’t be shadowing variables between the object and the class. This is caused a lot of confusion.
Definitely watch the videos. Especially with the new book, the videos are way better. Higher-quality, more content, initially a lot more interactive things like breaking code fixing it errors etc.