Question about Objects - repost with correct email account

B

bas68

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 ====
 
F

Francis Barnhart

Because getSomething is a method you need to use:

--
Francis Barnhart
(e-mail address removed)
http://francisbarnhart.com
AIM: FrAniMaL

"They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety."
-- Benjamin Franklin, Historical Review of Pennsylvania, 1759
 
T

Thorsten Pferdekämper

bas68 said:
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 ====
x = Test("Microsoft Sucks")
x.getSomething
<bound method Test.getSomething of <__main__.Test instance at 0x00C01940>>
x.something 'Microsoft Sucks'

Hi,
getSomething is a callable attribute of class Test (i.e. a method),
so the expression:
x.getSomething
evaluates to the method itself, while
x.getSomething()
calls the method and therefore evaluates to the result of the method.
Regards,
Thorsten
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top