Question about Objects

C

campbell95

I've been hacking visual basic for several years and understand the basic
concepts of OOP. That said, I'm stumped here with the Python Class.

Here is the Class...
class Test:
def __init__(self, something):
self.something = something

def getSomething(self):
return self.something

This is what I get when I test it. Why does <getSomething> not return the
value of <something>? is obvious that <something> has a value. I fear this
is a simple oversight but I've racked my brain for hours looking at online
doc's and examples. Thanks for any help!!
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
IDLE 1.0 ==== No Subprocess ====
 
M

Mark Roach

I've been hacking visual basic for several years and understand the basic
concepts of OOP. That said, I'm stumped here with the Python Class. [...]
def getSomething(self):
return self.something [...]
x.getSomething
<bound method Test.getSomething of <__main__.Test instance at 0x00C01940>>

you need to actually call the method: x.getSomething()

-Mark
 
F

Fernando Rodriguez

This is what I get when I test it. Why does <getSomething> not return the
value of <something>? is obvious that <something> has a value. I fear this
is a simple oversight but I've racked my brain for hours looking at online

It is. ;-)
doc's and examples. Thanks for any help!!
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
IDLE 1.0 ==== No Subprocess ====
x = Test("Microsoft Sucks")
x.getSomething
<bound method Test.getSomething of <__main__.Test instance at 0x00C01940>>

Remember, this isn't VB: you can't leave the parens off when calling a method.
There's no difference between subs and functions in Python, and you must
include the parens if you want to _call_ the function and get the value.
Otherwise you get the function.

It makes sense, it just that you're used to the peculiar MS-way of doing
things. Your mind will heal, don't worry. ;-)
 
B

bas68

Great community, glad I stopped in. Thanks for all the help!

Fernando Rodriguez said:
This is what I get when I test it. Why does <getSomething> not return the
value of <something>? is obvious that <something> has a value. I fear this
is a simple oversight but I've racked my brain for hours looking at
online

It is. ;-)
doc's and examples. Thanks for any help!!
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
IDLE 1.0 ==== No Subprocess ====

x = Test("Microsoft Sucks")
x.getSomething
<bound method Test.getSomething of <__main__.Test instance at
0x00C01940>>

Remember, this isn't VB: you can't leave the parens off when calling a method.
There's no difference between subs and functions in Python, and you must
include the parens if you want to _call_ the function and get the value.
Otherwise you get the function.

It makes sense, it just that you're used to the peculiar MS-way of doing
things. Your mind will heal, don't worry. ;-)
 
B

Bruno Desthuilliers

bas68 said:
Great community, glad I stopped in. Thanks for all the help!
You're welcome !-) And yes, this is probably one of the friendliest
places on usenet (thanks you all python-lovers BTW).

(But please edit out irrelevant parts of the posts you're answering to
!-)

(snip other posts)

Bruno
 

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

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top