471,310 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Accessing attributes?

Hi there.

I'm working with the Python Tutorial "Byte of Python" at swaroopch.info.

I have created the following file:

------------------------------------------------------------------------
#!/usr/bin/env python
# Filename: objvar.py

class Person:
"""Represents a person."""
population = 0 # class variable

def __init__(self,name):
"""Initializes the person's data."""
self.name = name # object variable
print '(Initializing %s)' % self.name

# When this person is created, he/she adds to the population
Person.population += 1

def __del__(self):
"""I am dying."""
print '%s says bye.' % self.name

Person.population -= 1

if Person.population == 0:
print 'I am the last one.'
else:
print 'There are still %d people left.' % Person.population

def say_hi(self):
"""Greeting by the person.

Really, that's all it does."""
print 'Hi, my name is %s.' % self.name

def how_many(cls):
"""Prints the current population."""
if Person.population == 0:
print 'Nobody is alive as of now.'
elif Person.population == 1:
print 'There is just one person here.'
else:
print 'We have %d persons here.' % Person.population
how_many = classmethod(how_many)

#jeff = Person('Jeff')

#jeff.say_hi()

cj = Person('Calamity Jane')
#cj.say_hi()
Person.how_many()

#jeff.say_hi()
Person.how_many()
---------------------------------------------------------------------------------

but when I execute:

% objvar.py

I get the error message:

(Initializing Calamity Jane)
Traceback (most recent call last):
¬* File "/home/jef/bin/objvar.py", line 49, in <module>
¬* ¬* Person.how_many()
AttributeError: class Person has no attribute 'how_many'

Where am I going wrong?

Many TIA for any help.
Jun 12 '07 #1
2 1040
In <tZ******************************@pipex.net>, Jeff Rollin wrote:
(Initializing Calamity Jane)
Traceback (most recent call last):
¬* File "/home/jef/bin/objvar.py", line 49, in <module>
¬* ¬* Person.how_many()
AttributeError: class Person has no attribute 'how_many'

Where am I going wrong?
Looking at the indention of the code I would say you just have an
`__init__()` and all other ``def``\s are local functions to that method.

Ciao,
Marc 'BlackJack' Rintsch
Jun 12 '07 #2
On Jun 12, 5:04 am, Jeff Rollin <jeffrey.rol...@gmail.comwrote:
Where am I going wrong?

Many TIA for any help.
Look at your code, then look at swaroop's

http://tinyurl.com/2v5zze

Line up all your defs at the same indent and they should work.
Jun 12 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mike Clair | last post: by
7 posts views Thread by rein.petersen | last post: by
Rafael Justo
2 posts views Thread by Rafael Justo | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.