Question about Objects

SBrunning at trisystems.co.uk SBrunning at trisystems.co.uk
Fri Nov 21 10:00:19 EST 2003


> From:	campbell95 [SMTP:campbell95 at cox.net]
> 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'
> >>>>
 
getSomething *will* return something - but you have to *call* it.

See, a function (or method, a.k.a a bound method) is also an object.
"x.getSomething" with no brackets at the end just *refers* to this object.
To call the function, try "x.getSomething()".

This can be very useful at times, 'cos it allows you to pass functions
around the same way you do with any other object types. But it also confuses
VBers. ;-)

Cheers,
Simon Brunning,
http://www.brunningonline.net/simon/blog/
--LongSig





-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.





More information about the Python-list mailing list