a beginner, beginners question

John Machin sjmachin at lexicon.net
Wed Mar 19 12:09:27 CET 2008


On Mar 19, 9:40 pm, klaus <k... at us.de> wrote:
> Hello,
>
> I'm trying to learn python programming and so far so good. However when
> trying to master the oop side I ran into a small problem.
>
> I think I've done everything ok as outlined below. But I just don't
> understand why the `method' of `class' example isn't printing any of the
> variables that I have set earlier on ? Could someone please explain to me
> what it is I am doing wrong ?
>
> Thank you so much !
>
> #!/usr/bin/env python
>
> class example:

What book or online tutorial are using that doesn't lead you to
express that as:
    class Example(object):
??


>     def __init__(self, foo, bar):
>         self.foo = foo
>         self.bar = bar
>
>     def method(self):
>         print "method ... :"
>         print self.foo
>         print self.bar
>
> if __name__ == "__main__":
>     obj = example
To see what is happening here, do:
    print repr(obj)
You need to CALL the class:
    obj = example()

>     obj.foo = "var1"
>     obj.bar = "var2"
>     obj.method
This expression produces the method itself, then throws it away.
You need to CALL the method:
   obj.method()

HTH,
John



More information about the Python-list mailing list