call by reference howto????

castironpi at gmail.com castironpi at gmail.com
Fri Feb 29 07:54:29 EST 2008


On Feb 29, 5:56 am, Steve Holden <st... at holdenweb.com> wrote:
> castiro... at gmail.com wrote:
> > On Feb 27, 6:02 pm, Tamer Higazi <n... at mail.de> 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)
>
> regards
>   Steve
> --
> Steve Holden        +1 571 484 6266   +1 800 494 3119
> Holden Web LLC              http://www.holdenweb.com/- Hide quoted text -
>
> - 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!



More information about the Python-list mailing list