creating and naming objects

bruno at modulix onurb at xiludom.gro
Wed Jun 7 20:25:23 CEST 2006

Brian wrote:
> Thank you all for your response.  I think that I am getting it.  Based
> on those responses, would I be correct in thinking that this would be
> the way to initialize my Student object and return the values?
> class Student:

Do yourself a favour: use new-style classes

class Student(object):

>     def __init__(self, name, id):
> = name
> = id

ok until here.

>     def getName(self):
>         return
>     def getId(self):
>         return

These to getters are totally useless in Python. Python doesn't have
access restrictors [1], so you can directly access the attributes.
Python also has support for computed attributes (looks like an ordinary
attribute but is accessed thru a getter/setter), so there's no problem
wrt/ encapsulation. Just get rid of these two methods and you'll be fine.

[1] it's on purpose - we prefer to use conventions like using
_names_starting_with_a_leading_underscore to denote implementation stuff
that should not be accessed by client code.

> Additionally, correct me if I am wrong but I can recycle:
> foo = Student()

This won't work - will raise a TypeError. You defined Student.__init__()
to take 2 mandatory parameters name and id,so you must pass them at call

FWIW, you would have noticed this just by trying it into your Python
shell. The interactive Python shell is a very valuable tool. It makes
exploring APIs and testing ideas a breeze, with immediate feedback. It
is one of the things that makes Python so easy and usable (not that it's
a Python invention - but it's a GoodThing(tm) Python have it).

> And doing so will just create a new instance of Student (with whatever
> attributes it is given) and the original foo is gone?

Yes. Or more exactly, the identifier 'foo' will refer to the newly
created Student object, and if there are no other references to the
object previously pointed by 'foo', it will be disposed of...

> Thanks for your help and patience.  After reading my original post and
> then this one, I could see that it may look suspiciously like a home
> work assignment.  Trust me it's not.

It could have been homework, this is not a problem - as long as you
effectively tried to do your homework by yourself, and only ask for help
when stuck. FWIW, in this job, knowing when and how to ask for help is
as important as knowing how to first try to solve the problem oneself !-)

bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"

More information about the Python-list mailing list