Class Help

marduk usenet at
Sun Oct 2 01:14:54 CEST 2005

On Sat, 2005-10-01 at 18:58 -0400, Ivan Shevanski wrote:
> To continue with my previous problems, now I'm trying out classes.  But I 
> have a problem (which I bet is easily solveable) that I really don't get.  
> The numerous tutorials I've looked at just confsed me.For intance:
> >>>class Xyz:
> ...     def y(self):
> ...             q = 2
> ...
> >>>Xyz.y()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: unbound method y() must be called with Xyz instance as first 
> argument
> (got nothing instead)
> So. . .What do I have to do? I know this is an extremley noob question but I 
> think maybe if a person explained it to me I would finally get it =/

When you define a class, say Xyz, your are defining your own type.  Say
that Person is a class.  And person has a method walk():

class Person:
    def walk(self):

now to use the Person class, you need to create an instance of it.  You
can't just say Person.walk() because Person is a "class"ification, not a
real object.  You need an instance of person.

jane = Person()

This creates a new person called "jane".  "jane" is an instance of the
class Person.

>>> jane
 <__main__.Person instance at 0x2aaaac723710>

Now we can tell jane to walk:


So what the error is telling you (in a bit confusing way if you're a
newbie) is that you are calling a method y() but you have not created an
instance of your Xyz class to do y().  Or, to use my analogy, you are
telling Person to walk, but you can't make Person walk, you have to
create a person, jane, and have jane walk.

Hope this helps, but I recommend you read a good intro to
object-oriented programming.


More information about the Python-list mailing list