Re: call by reference howto????
Steven D'Aprano [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | On Sat, 15 Mar 2008 11:50:17 -0700, Dennis Lee Bieber wrote: | | Small integers are cached in Python, so they always have a fixed ID | (address). | | Small integers are cached in CPython, making it an implementation- | dependent feature. Indeed, the set cached has changed from version to version. | I don't believe that caching is promised by the | language definition, No, could even disappear from cPython. -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On Mar 15, 12:39 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On 14 Mar 2008 10:38:52 GMT, Antoon Pardon [EMAIL PROTECTED] declaimed the following in comp.lang.python: In that case I find it very strange that when this question comes up, I see so few attempts to explain how the assignment acctually works. I'll probably get burned for this... but as far as I'm concerned, assignment works very simply... At the very bottom of the name resolution, assignment works by changing the left-hand-side reference from any prior object for whatever object represents the result of the right-hand-side. A bare LHS name is rebound in total. lhs_name = rhs_expresssion A qualified LHS name (a name followed by a .subname, or a [index], or a [keyvalue]) rebinds the specified subcomponent of name -- that is, name remains bound to some object, and the .subname (or [index] or [keyvalue]) component of that object is what is rebound to the RHS object. lhs_name.subcomponent = rhs_expression lhs_name[index] = rhs_expression lhs_name[keyvalue] = rhs_expression No matter how many . or [] sets appear, at the very bottom one has focused down to a component, and that component get rebound from one object to another object. In no case does the object originally bound get changed. FOR assignment... Mutables using methods are a bit different... lhs.append(rhs) #assume a list adds a new element into the list, but it is still the same list (and in that aspect, it is also a qualified name). You could warn if a name is assigned to twice-- maybe even error. Assignment is not a method. a= object() a= object() a is a name. it is a name of an object. what name? a. what object? that changes on demand. a= 2 a+= 2 a 4 a= (2,) a+= (3,) a (2, 3) a= 2 id( a ) # 1 505252512 a+= 2 id( a ) 505252544 # != a 4 a= (2,) id( a ) # 2 11863472 a+= (3,) id( a ) # != 11933976 a= [2] id(a) # 3 11935296 a+= [3] id(a) # == 11935296 += changed the id of a in the first two cases. It did not in the third. Assignment is not a method. Sometimes augmented assignment is. In the first two cases, the original was destroyed. In the third, the newcomer was destroyed. The newcomer was also destroyed in the first two. If you can write 'a' on an envelope and it goes to 244 White St., then assigning to a is an order to the post office: send 'a' to somewhere else. It doesn't change what it referred to before, though-- those letters are already delivered! There is ambiguity in 'change a'-- it can mean 'at the post office' or 'wherever letters address to a go to'. However, you can't change the entry of a with the post office of another scope. I want 'a' to be [3]. AmbiguityError: Use 'rearrange' or 'reroute'. Rearrange 'a'. Move out and move 3 in. a[:]= [3] Reroute 'a'. Move 3 in to the place wither mail to 'a' will go. a= [3] Some buildings have no entries at the post office. Some names don't either. You can change the contents of a building if you can send mail to it-- send a letter that says to change the contents! Other post offices keep their entries. Change another post office's entry for 'a'. No. Change mine. Ok! -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On 28 Feb, 02:24, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: Python doesn't do call by reference. Nor does it do call by value. Please pay no attention to anyone who says it does. Exactly. Python pass variables the same way as Lisp, which is neither call-by-value (cf. C) nor call-by-reference (cf. Fortran). The Python equivalent of pass by reference is a function that returns its arguments to the caller: def foobar(arg1, arg2, arg3): # Mutate a shared object. # I.e. calling convention cannot be not pass-by-value arg1.whatever = 1 # Rebind in the local namespace. # I.e. calling convention cannot be pass-by-value arg1, arg2, arg3 = 1, 2, 3 # Return rebound variables to achieve the effect of # pass-by-reference: return (arg1, arg2, arg3) # Call the function foobar, and rebind its arguments to its # return values: arg1, arg2, arg3 = foobar(arg1, arg2, arg3) -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On 28 Feb, 02:24, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: Python doesn't do call by reference. Nor does it do call by value. Please pay no attention to anyone who says it does. Exactly. Python pass variables the same way as Lisp, which is neither call-by-value (cf. C) nor call-by-reference (cf. Fortran). The Python equivalent of pass by reference is a function that returns its arguments to the caller: def foobar(arg1, arg2, arg3): # Mutate an object shared with the caller. # I.e. calling convention cannot be not pass-by-value arg1.whatever = 1 # Rebind variables in the local namespace. # I.e. calling convention cannot be pass-by-reference arg1, arg2, arg3 = 1, 2, 3 # Return rebound variables to achieve the effect of # pass-by-reference: return (arg1, arg2, arg3) # Call the function foobar, and rebind its arguments to its # return values: arg1, arg2, arg3 = foobar(arg1, arg2, arg3) -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On 13 Mar, 09:22, Antoon Pardon [EMAIL PROTECTED] wrote: Whatever python has for a calling convention, it is close enough that naming it call by reference gives people a reasonable idea of what is going on. Only to the extent that many mistake passing Java or C# reference types for call-by-reference. Call by reference is the calling convention used by Fortran, Pascal and Visual Basic 6. If you don't know any of these languages, chances are you don't understand what call by reference really means. -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On Sat, 15 Mar 2008 11:50:17 -0700, Dennis Lee Bieber wrote: Small integers are cached in Python, so they always have a fixed ID (address). Small integers are cached in CPython, making it an implementation- dependent feature. I don't believe that caching is promised by the language definition, and while CPython has a special relationship to Python-the-language, I don't think an implementation that failed to cache small integers would be described as not Python. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On 2008-03-13, Terry Reedy [EMAIL PROTECTED] wrote: Antoon Pardon [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | On 2008-02-28, Steven D'Aprano [EMAIL PROTECTED] wrote: | 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. | | Whatever python has for a calling convention, it is close enough that | naming it call by reference gives people a reasonable idea of what | is going on. But it is also different enough to mislead people. IMO the confusing come more from people expecting the assignment to be some kind of mutating operation. | AFAICS people don't have a problem with understanding the calling | convention of python. They have a problem understanding the | assignment semantics. The calling convention is cross-namespace assignment. So one cannot understand calls without understanding assignment. In that case I find it very strange that when this question comes up, I see so few attempts to explain how the assignment acctually works. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On 2008-03-13, Steven D'Aprano [EMAIL PROTECTED] wrote: On Thu, 13 Mar 2008 08:54:43 +, Paul Rudin wrote: Whatever python has for a calling convention, it is close enough that naming it call by reference gives people a reasonable idea of what is going on. Quite. The thing is not to get hung up with the label - but to understand what actually happens. Agreed. And I think the best way to do that is to use the same terms that other languages use to get very different behaviour indeed. Python is call by reference, but this doesn't work like call by reference in all the other languages I've used: def negate(n): n = -n x = 2 negate(x) assert x == -2 Is this a bug, or is Python actually call by value? And now comes the explanations that Python is actually call by value, but the values being copied are *pointers* (ignoring Jython, IronPython and PyPy); or that Python is really call by reference, but different things (it's never quite clear what exactly) happen depending on whether the arguments are mutable or immutable; Which is wrong. The same thing happens with mutable and immutable objects. What one needs to understand is: Right after the call, x and n are the same object. After the assigment x and n no longer are the same object. The assignment doesn't negate the original object. It creates a new object with the negative value of the original while the latter remains bound to x. As long as the person who has trouble with this behaviour doesn't understand that the assignment is not a mutating operator. Your explanation will get you nowhere. The trouble is mosly caused because people see a line like n = -n and interpret this more or less as the object bound to n has the negated value after the assignment. That is why they think x should be -2 in your example. or that if you define reference broadly enough, everything is a reference; or that if you define value broadly enough, everything is a value; or if schools would just stop teaching kiddies C and start teaching them Lisp, call by reference semantics would be understood in a different way, or or or or... And thus, by insisting that there are two and only two calling conventions, no matter how many different kinds of calling behaviour actually exist, we ensure that we'll be having this same *$%^*# argument when Python 4000 comes out. But you seem to make the same kind of mistake, but with regard to assignments. You seem to implicitly assume there is only one kind of assignment and by trying to start from there and trying to explain what goes on in terms of different calling conventions you will ensure the same argument just as well. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On 2008-02-28, Steven D'Aprano [EMAIL PROTECTED] wrote: 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. Whatever python has for a calling convention, it is close enough that naming it call by reference gives people a reasonable idea of what is going on. Instead, read this: http://effbot.org/zone/call-by-object.htm Which IMO isn't very helpfull. The mentioning of e.g. mutable just confuses things since it really isn't the issue. AFAICS people don't have a problem with understanding the calling convention of python. They have a problem understanding the assignment semantics. It is just that these problems of understanding most often surface when an assignment is made to a parameter which makes them think they don't understand the calling convention. The important thing to understand is that an assigment in python is not copying the contends into the variable but is passing a reference/binding the name to a new object. After the variable is assigned to it doesn't refer to the same object with a new contend. It refers to a different object. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
Antoon Pardon [EMAIL PROTECTED] writes: On 2008-02-28, Steven D'Aprano [EMAIL PROTECTED] wrote: 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. Whatever python has for a calling convention, it is close enough that naming it call by reference gives people a reasonable idea of what is going on. Quite. The thing is not to get hung up with the label - but to understand what actually happens. -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
Antoon Pardon [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | On 2008-02-28, Steven D'Aprano [EMAIL PROTECTED] wrote: | 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. | | Whatever python has for a calling convention, it is close enough that | naming it call by reference gives people a reasonable idea of what | is going on. But it is also different enough to mislead people. | AFAICS people don't have a problem with understanding the calling | convention of python. They have a problem understanding the | assignment semantics. The calling convention is cross-namespace assignment. So one cannot understand calls without understanding assignment. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On Thu, 13 Mar 2008 08:54:43 +, Paul Rudin wrote: Whatever python has for a calling convention, it is close enough that naming it call by reference gives people a reasonable idea of what is going on. Quite. The thing is not to get hung up with the label - but to understand what actually happens. Agreed. And I think the best way to do that is to use the same terms that other languages use to get very different behaviour indeed. Python is call by reference, but this doesn't work like call by reference in all the other languages I've used: def negate(n): n = -n x = 2 negate(x) assert x == -2 Is this a bug, or is Python actually call by value? And now comes the explanations that Python is actually call by value, but the values being copied are *pointers* (ignoring Jython, IronPython and PyPy); or that Python is really call by reference, but different things (it's never quite clear what exactly) happen depending on whether the arguments are mutable or immutable; or that if you define reference broadly enough, everything is a reference; or that if you define value broadly enough, everything is a value; or if schools would just stop teaching kiddies C and start teaching them Lisp, call by reference semantics would be understood in a different way, or or or or... And thus, by insisting that there are two and only two calling conventions, no matter how many different kinds of calling behaviour actually exist, we ensure that we'll be having this same *$%^*# argument when Python 4000 comes out. And for that we can all be grateful. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
[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) regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
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) 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! -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
[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. Hope this helps. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
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
Re: call by reference howto????
In article [EMAIL PROTECTED], Steve Holden [EMAIL PROTECTED] wrote: 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. ...unless thang is a new-style class using __getattribute__()... (Just a drive-by nitpicking. ;-) -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ All problems in computer science can be solved by another level of indirection. --Butler Lampson -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
[EMAIL PROTECTED] wrote: On Feb 27, 10:38 pm, Dan Bishop [EMAIL PROTECTED] 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.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
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' -- http://mail.python.org/mailman/listinfo/python-list
call by reference howto????
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
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-object.htm and possibly this as well: http://effbot.org/zone/python-objects.htm After you've read that, if you still have questions, please ask. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
En Wed, 27 Feb 2008 22:02:19 -0200, Tamer Higazi [EMAIL PROTECTED] 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-objects.htm and then this one http://effbot.org/zone/call-by-object.htm There is a FAQ entry at http://www.python.org/doc/faq/programming/#how-do-i-write-a-function-with-output-parameters-call-by-reference The Google interface is good for searching past messages on this topic: http://groups.google.com/group/comp.lang.python/ -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On Feb 27, 5: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 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
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. 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On Feb 27, 10:38 pm, Dan Bishop [EMAIL PROTECTED] 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! -- http://mail.python.org/mailman/listinfo/python-list