473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1117
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.