[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

call by reference howto????

Andreas Harlander

2/28/2008 12:02:00 AM

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
26 Answers

Steven D'Aprano

2/28/2008 1:25:00 AM

0

On Thu, 28 Feb 2008 02:02:19 +0200, Tamer Higazi wrote:

> Hi!
> Can somebody of you make me a sample how to define a function based on
> "call by reference" ???

Python doesn't do call by reference. Nor does it do call by value. Please
pay no attention to anyone who says it does.

Instead, read this:

http://effbot.org/zone/call-by-...

and possibly this as well:

http://effbot.org/zone/python-o...

After you've read that, if you still have questions, please ask.



--
Steven

Gabriel Genellina

2/28/2008 1:27:00 AM

0

En Wed, 27 Feb 2008 22:02:19 -0200, Tamer Higazi <no@mail.de> escribió:

> 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.

First read this article http://effbot.org/zone/python-o... and then
this one http://effbot.org/zone/call-by-...

There is a FAQ entry at
http://www.python.org/doc/faq/programming/#how-do-i-write-a-function-with-output-parameters-call-by...

The Google interface is good for searching past messages on this topic:
http://groups.google.com/group/comp.la...

--
Gabriel Genellina

7stud --

2/28/2008 4:09:00 AM

0

On Feb 27, 5:02 pm, Tamer Higazi <n...@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

def my_func(x):
x[0] = 4


my_list = [3]
print my_list

my_func(my_list)
print my_list

--output:--
[3]
[4]



class MyData(object):
pass

def another_func(obj):
obj.val += 10


md = MyData()
md.val = 20
another_func(md)
print md.val

--output:--
30

Dan Bishop

2/28/2008 4:39:00 AM

0

On Feb 27, 6:02 pm, Tamer Higazi <n...@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.

You don't.

What exactly are you wanting to do? Could you write some pseudocode
for the function to which you'd like to pass by reference.

Aaron Brady

2/28/2008 7:44:00 AM

0

On Feb 27, 10:38 pm, Dan Bishop <danb...@yahoo.com> wrote:

> What exactly are you wanting to do?

I'm having a hard time considering your question in the general case.
I'm thinking of too many cases, the details of which are relevant to
the answer, to even subdivide them. My specialty is specific
application; I know ten tricks that apply 10% of the time each. I can
show you all of them but not at once!

Steve Holden

2/28/2008 1:28:00 PM

0

castironpi@gmail.com wrote:
> On Feb 27, 10:38 pm, Dan Bishop <danb...@yahoo.com> wrote:
>
>> What exactly are you wanting to do?
>
> I'm having a hard time considering your question in the general case.
> I'm thinking of too many cases, the details of which are relevant to
> the answer, to even subdivide them. My specialty is specific
> application; I know ten tricks that apply 10% of the time each. I can
> show you all of them but not at once!

When you have nothing to say it's normally best not to say it.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

Aaron Brady

2/28/2008 7:52:00 PM

0

On Feb 27, 6:02 pm, Tamer Higazi <n...@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'

Steve Holden

2/29/2008 11:57:00 AM

0

castironpi@gmail.com wrote:
> On Feb 27, 6:02 pm, Tamer Higazi <n...@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.hold...

Aaron Brady

2/29/2008 12:54:00 PM

0

On Feb 29, 5:56 am, Steve Holden <st...@holdenweb.com> wrote:
> castiro...@gmail.com wrote:
> > On Feb 27, 6:02 pm, Tamer Higazi <n...@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.holde... 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!

Steve Holden

2/29/2008 2:13:00 PM

0

castironpi@gmail.com wrote:
> On Feb 29, 5:56 am, Steve Holden <st...@holdenweb.com> wrote:
>> castiro...@gmail.com wrote:
>>> On Feb 27, 6:02 pm, Tamer Higazi <n...@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)
>>
[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.

Hope this helps.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...