I'm learning python out of "core python programming 2nd edition"
Im stuck at the part "How to Create Class instances". Page 84.
The example is as follows:
classes.py:
class FooClass(object):
"""my very first class: FooClass"""
version = 0.1 # class (data) attribute
def __init__(self, nm='John Doe'):
"""constructor"""
self.name = nm # class instance (data) attribute
print'Created a class instance for', nm
def showname(self):
"""display instance attribute and class name"""
print 'Your name is', self.name
print 'My name is', self.__class__.__name__
def showver(self):
"""display class(static) attribute"""
print self.version # references FooClass.version
def addMe2Me(self, x): # does not use 'self'
"""apply + operation to argument"""
return x + x
Then i have to create Class Instances:
in my interpreter i do the follow:
Import classes *
fool = FooClass()
But nothing happends. It should print the init.
also when i use
fool.showname() and fool.showver it doesn't print any. It says
FooClass' object has no attribute 'showver
i really want to know what going on here, before i proceed. Hopefully someone can help me out!
Thanks in advance! :)
importwith capital I or lower case i?deflines are at the same level as theclass? If so, they don't belong to the class and are functions, not methods.