[Tutor] static method

Yanko, Curtis (GSP) YankoC@gspinc.com
Tue, 14 Dec 1999 09:06:27 -0500


How can we use this to implement a Singleton Pattern?

> -----Original Message-----
> From:	Christian Tismer [SMTP:tismer@appliedbiometrics.com]
> Sent:	Tuesday, December 14, 1999 8:23 AM
> To:	Joseph J. Strout
> Cc:	Sergio Murru; tutor@python.org
> Subject:	Re: [Tutor] static method
> 
> 
> 
> "Joseph J. Strout" wrote:
> > 
> > At 4:18 PM +0100 12/8/99, Sergio Murru wrote:
> > 
> > >The question is: How can I implement a static method in python??
> > 
> > You can't.
> 
> You can. In addition to Joe's advice which is right, I'd like to
> explain:
> 
> There is no direct support for class methods. Functions which appear
> to be defined in a class context are meant as methods which expect
> an instance of the class or a subclass as first parameter.
> 
> When a method is called from an instance, a bound method is
> created with the usual "self" parameter set to the instance.
> 
> When a method is called from a class, this binding does not
> happen, and you need to supply an instance of a subclass
> as first parameter explicitly.
> 
> So far about the standard.
> But there are exceptions, for instance (for class:)
> 
> >>> class funny:
> ... 	len = len
> ... 
> >>> funny.len("weird")
> 5
> >>> 
> 
> Ok, this is for builtin functions. For Python functions, the
> following trick is possible (although I fear Guido's slap:)
> 
> >>> class class_method:
> ... 	def blush(self, arg):
> ... 		return arg+13
> ... 	
> >>> class funny:
> ... 	cmeth = class_method().blush
> ... 
> >>> funny.cmeth(29)
> 42
> >>> 
> 
> What is going on here?
> The point is that a python function will be bound to a method
> once and only once. This is done by the class_method() instance
> creation. It will not happen again, and when I assign this
> bound method in my funny class, the binding has been satisfied
> already, and calling funny.cmeth is like calling an ordinary
> function.
> 
> Not to say that one should do this, but it is in fact possible.
> 
> ciao - chris
> 
> -- 
> Christian Tismer             :^)   <mailto:tismer@appliedbiometrics.com>
> Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
> Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
> 10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
> PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
>      we're tired of banana software - shipped green, ripens at home
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor