a beginner, beginners question

K

klaus

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:
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
obj.foo = "var1"
obj.bar = "var2"
obj.method
 
T

Tim Chase

class example:
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

This makes "obj" a synonym for "example". You want to
instantiate your "example" class with

obj = example("Some foo", "Some Bar")
obj.foo = "var1"
obj.bar = "var2"

which then makes these two lines either unneeded or an
overwriting of the initialization
obj.method

and this returns a function, not the results of the function.
You need to call it

obj.method()

-tkc
 
J

John Machin

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
 
K

klaus

This makes "obj" a synonym for "example". You want to instantiate your
"example" class with

obj = example("Some foo", "Some Bar")


which then makes these two lines either unneeded or an overwriting of
the initialization


and this returns a function, not the results of the function. You need
to call it

obj.method()

-tkc

Ok thank you for elaborating I think I've got it.

Thnkx. !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top