newbie questions

houbahop d.lapasset
Sat Dec 11 13:45:15 EST 2004


Hi,

"Steven Bethard" <steven.bethard at gmail.com> a écrit dans le message de news: 
bvCud.743222$8_6.73414 at attbi_s04...
> houbahop wrote:
>> Thank you everyone, but I still not understand why such a comon feature 
>> like passing parameters byref that is present in most serious programming
>> languages is not possible in a clean way,here in python.
>
> I understand from this statement that Java is not a serious programming 
> language? ;)

;) I didn't want to say that.
In fact, python is the first language I use that is like java 100% oriented 
object.
I didn't realise that the main function, was an object too ! (and I don't 
really knew that self.something was meaning that it was a class attribute.. 
just grabed this in a code found on the net :) )
My habit in VB was to start a new project with a module (that is not an 
object and doesn't have class functions) and then calling object functions 
and passing them local to the main  objects.

like this :

class a()
    blabla
class b()
    blabla

main()
   create a instance
   create b instance
  b()
  a(b)
end main

But in the very small script that I'm coding, I don't really need OOP and I 
will not use it even if I really like that.



> I don't see why lack of pass-by-reference would force you to use 
> globals...
> Or by globals do you mean instance variables?  If you don't want any 
> instance variables (which means you don't really want OO), you can still 
> clear your list as long as you have any name bound to the list object:

You're right.

> >>> def clear(lst):
> ...     while lst:
> ...         lst.pop()
> ...
> >>> x = [pow(x, 7, 19) for x in range(10)]
> >>> x
> [0, 1, 14, 2, 6, 16, 9, 7, 8, 4]
> >>> clear(x)
> >>> x
> []
>
> or alternatively:
>
> >>> def clear(lst):
> ...     lst[:] = []
> ...
> >>> x = [pow(x, 7, 19) for x in range(10)]
> >>> x
> [0, 1, 14, 2, 6, 16, 9, 7, 8, 4]
> >>> clear(x)
> >>> x
> []
>
> Note that neither of these functions requires pass-by-reference; the lst 
> local in the function is not the same name as the x local outside the 
> function.  But since you're basically just passing a "pointer" by value, 
> both "variables" still "point" to the same object

Passing a pointer by value appears to me as passing a var by reference.

(or in Python terms,
> both "names" are "bound" to the same object).  To apply an affect that is 
> visible to all names bound to an object, you simply need to mutate the 
> object.  In the cases above, this is just a matter of using the 
> appropriate object method (list.pop or list.__setslice__ respectively 
> above).

Thanks I will try all of that, but what does really means mutating in 
python? It's the first time I hear this word in programming :))
Dominique





More information about the Python-list mailing list