get a self data in a method call

Erik Max Francis max at alcyone.com
Sun Dec 22 18:37:48 EST 2002


polux wrote:

> I've a class
> 
> class MyClass:
> 
>         def __init__(self):
>                 self.data = 123
> 
> now, I want to define a method, which would be like that if it were
> possible :
> 
>         def func(self, data = self.data )
> 
> but it doesn't work because python does't know yet what "self" is

Try using a sentinel, like None, to represent that the caller didn't
pass anything in, and then act accordingly:

	def func(self, data=None):
	    if data is None:
	        self.data = data
	    ...

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ God will forgive me; that's his business.
\__/ Heinrich Heine
    Maths reference / http://www.alcyone.com/max/reference/maths/
 A mathematics reference.



More information about the Python-list mailing list