472,988 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 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 1107
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JM | last post by:
Hello everyone, Does anybody know about, have documentation on, or have any code samples on how to access class members from a python class in C++. Say I have a simple python script: ...
2
by: Marc A. Criley | last post by:
First off, I'm an experienced software developer but new to XML, so be gentle :-) I've put together a schema that defines optional attributes for some of the elements. Some of these attributes...
1
by: wooks | last post by:
I have some information embedded in included schemas which I want to access at run-time for the purposes of contructing a GUI (they will support field labels and tool tips). The options seem to...
4
by: Mike Clair | last post by:
Hey, I'm having an issue with CSS, JS and the DOM. It's gonna drive me batty. I am trying to access the properties of a layer in JS which have been initially set in an external CSS. The problem...
2
by: Erwin S. Andreasen | last post by:
Hi, I have a web application where window A opens window B (same site). B later wants to do something depending on whether the window A, window.opener.document.domain, has changed. However,...
2
by: damonf | last post by:
I'm currently trying to add an ASP hyperlink to a template column in a datagrid. The normal hyperlink column doesn't give me the ability to add attributes to the item. In my grid there are four...
2
by: Steve Black | last post by:
Hello, I am dynamically creating checkboxes on my web page based on data in a SQL Server table. Therefore, these checkboxes do not exist at design time and I cannot refer to them in my...
7
by: rein.petersen | last post by:
Hey All, I was wondering if there were a way for a script to access it's parent tag without having to use the document.all.tags method which doesn't necessarily identify it if there are more...
3
by: Beorne | last post by:
In the classes I develop my attributes are always private and are exposed using properties. directly or to access the attributes using the properties? Does "wrapper" setter/getter properties...
2
Rafael Justo
by: Rafael Justo | last post by:
Hi, I'm new in python development (NEWBIE). While I was using Cheetah Templates I got a problem about accessing template variables. I have an object like this (class Template): >>> class...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.