danny37415
June 29th, 2009, 08:29 PM
I've been reading A Byte of Python (the python3.0 version) and I do all the examples in them myself. I haven't had any trouble until now and was wondering if anyone could help me. I typed everything out exactly as in the book and tried to execute it and got this error.
Traceback (most recent call last):
File "objvar.py", line 42, in <module>
droid1 = Robot('R2-D2')
File "objvar.py", line 14, in __init__
print('Initializing {0}'.format(self.name))
AttributeError: 'str' object has no attribute 'format'
Exception exceptions.AttributeError: "'str' object has no attribute 'format'" in <bound method Robot.__del__ of <__main__.Robot instance at 0xb7dd2c0c>> ignored
Then I copied and pasted the example code from the book and got the same results.
Here's the code I typed out
#!/usr/bin/python3.0
# Filename : objvar.py
class Robot:
'''Represents a robot with a name'''
# A class variable, counting the number of robots.
population = 0
def __init__(self, name):
'''Initializes the data.'''
self.name = name
print('Initializing {0}'.format(self.name))
# When this person is created, the robot
# adds to the population
Robot.population += 1
def __del__(self):
'''I am dying'''
print('{0} is being destroyed'.format(self.name))
Robot.population -= 1
if Robot.population == 0:
print('{0} was the last one.'.format(self.name))
else:
print('There are still {0:d} robots working'.format(Robot.population))
def sayHi(self):
'''Greeting by the robot'''
print('Greetings, my masters call me {0}.'.format(self.name))
def howMany():
'''Prints the current population'''
print('We have {0:d} robots.'.format(Robot.population))
howMany = staticmethod(howMany)
droid1 = Robot('R2-D2')
droid1.sayHi()
Robot.howMany()
droid2 = Robot('C-3PO')
droid2.sayHi()
Robot.howMany
print('\nRobots can do some work here.\n')
print('Robots have finished their work. So let\' destroy them.')
del droid1
del droid2
Robot.howMany()
Also, does anyone have any other good python tutorials I could look at?
Traceback (most recent call last):
File "objvar.py", line 42, in <module>
droid1 = Robot('R2-D2')
File "objvar.py", line 14, in __init__
print('Initializing {0}'.format(self.name))
AttributeError: 'str' object has no attribute 'format'
Exception exceptions.AttributeError: "'str' object has no attribute 'format'" in <bound method Robot.__del__ of <__main__.Robot instance at 0xb7dd2c0c>> ignored
Then I copied and pasted the example code from the book and got the same results.
Here's the code I typed out
#!/usr/bin/python3.0
# Filename : objvar.py
class Robot:
'''Represents a robot with a name'''
# A class variable, counting the number of robots.
population = 0
def __init__(self, name):
'''Initializes the data.'''
self.name = name
print('Initializing {0}'.format(self.name))
# When this person is created, the robot
# adds to the population
Robot.population += 1
def __del__(self):
'''I am dying'''
print('{0} is being destroyed'.format(self.name))
Robot.population -= 1
if Robot.population == 0:
print('{0} was the last one.'.format(self.name))
else:
print('There are still {0:d} robots working'.format(Robot.population))
def sayHi(self):
'''Greeting by the robot'''
print('Greetings, my masters call me {0}.'.format(self.name))
def howMany():
'''Prints the current population'''
print('We have {0:d} robots.'.format(Robot.population))
howMany = staticmethod(howMany)
droid1 = Robot('R2-D2')
droid1.sayHi()
Robot.howMany()
droid2 = Robot('C-3PO')
droid2.sayHi()
Robot.howMany
print('\nRobots can do some work here.\n')
print('Robots have finished their work. So let\' destroy them.')
del droid1
del droid2
Robot.howMany()
Also, does anyone have any other good python tutorials I could look at?