On Feb 29, 8:12 am, Steve Holden <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > On Feb 29, 5:56 am, Steve Holden <[EMAIL PROTECTED]> wrote: > >> [EMAIL PROTECTED] wrote: > >>> On Feb 27, 6:02 pm, Tamer Higazi <[EMAIL PROTECTED]> wrote: > >>>> Hi! > >>>> Can somebody of you make me a sample how to define a function based on > >>>> "call by reference" ??? > >>>> I am a python newbie and I am not getting smart how to define functions, > >>>> that should modify the variable I passed by reference. > >>>> thanks in advance > >>>> Tamer > >>> If it's a mutable object, avoid the pitfalls of rebinding the > >>> parameter, and just modify the object. > >>> BAD: > >>> def f( a ): > >>> a= { 'this': 'that' } > >>> GOOD: > >>> def f( a ): > >>> a.clear() > >>> a[ 'this' ]= 'that' > >> BETTER: > > >> class Thang: pass > > >> def f(a): > >> a.this = "that" > > >> thang = Thang() > >> f(thang) > > [please refrain from quoting signatures in your replies] > [better still, use a mailer that omits them from the quote!] > > >> - Show quoted text - > > > What does __coerce__ look like, so you could operate on a.this without > > accessing 'this' member every time? For numbers maybe, but what about > > __getattr__( self, name ): return getattr( self.this, name ) for > > strings? Then thang.this= "that"; thang.find( 'at' ) -> > > thang.this.find( 'at' ). Awesome! > > Where did __coerce__ come from? Stick with the main party, please. > > __coerce__ is an old mechanism intended to be used to bring numeric > types into alignment, and AFAICS has nothing at all to do with whatever > idea you are suggesting. > > As near as I can make out you appear to want to have thang delegate > certain of its method to thang.this. The easiest way to do that would be > to implement a __getattr__() in the Thang class to do so, but remember > that it won't be called for cases where the class has a real attribute > with the correct name.
In your example, > class Thang: pass > > def f(a): > a.this = "that" > > thang = Thang() > f(thang) Thang -wasn't- doing anything else. It can delegate everything. (Thang& operator=( const Thang& );.) Then there's __getattribute__, which is called unconditionally, just in case you implement something in Thang besides 'this' on accident. -- http://mail.python.org/mailman/listinfo/python-list