Instantiating Classes in python (newbie)

infidel saint.infidel at gmail.com
Mon Oct 31 13:55:32 EST 2005


> Hello, I am learning python for work from knowing java,c#,c.  I had a
> couple questions.
> 1)  IntegerClass - to instantiate this class how come I use i =
> IntegerClass.IntegerClass() and then this works while i =
> IntegerClass() i.method.  I receive an error.

It depends on what IntegerClass is.  If you have a module named
IntegerClass with a class named IntegerClass within it, then you must
use IntegerClass.IntegerClass to refer to the class, because
IntegerClass is the module.  You could also change the import statement
to:

from IntegerClass import IntegerClass

... in which case, IntegerClass in your current namespace will be the
class and not the module.

This is all an educated guess because you didn't specify what the error
was.

> 2)  Also using self in the method (self, dataVal).  What does this do
> differently from using (dataval).  I am a little confused about the
> bound and unbound methods.  What does passing self do different?
> Thank-you for all your help

A bound method is kind of like an instance method in java, it's "bound"
to a particular instance object.  Python automatically inserts a
reference to the object itself as the first argument to a bound call.
"self" is just like "this" in Java, but you have to explicitly account
for it in the method signature.




More information about the Python-list mailing list