newbie questions

Mel Wilson mwilson at the-wire.com
Sat Dec 11 14:31:31 CET 2004


In article <pXBud.87963$ha.26614 at news.chello.at>,
"houbahop" <d.lapasset[Remove me)@chello.fr> 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 have the habit to never use globals as far as possible and this involve
>that my main function is passing some parameters by reference to subs to
>allow them to modify the vars.
>
>I would be sad to break my structured programming scheme because a lack of
>feature.
>
>In others languages you can use things like pointers to strings or
>Mysub(byref MyVar) ....
>
>and it does the trick :)

   It isn't a problem with passing by reference.  The
passing-by-reference part works just fine.  Putting in a
print statement to trace what's actually happening:


Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1,2,3,4,5]
>>> def X(s):
...     for i in xrange (len (s)):
...             del s[i]
...             print 'X:', s
...
>>> X(a)
X: [2, 3, 4, 5]
X: [2, 4, 5]
X: [2, 4]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in X
IndexError: list assignment index out of range
>>> a
[2, 4]
>>>


   As the last line shows, lots of things got removed from
`a` .. but not everything.  Roel Schroeven explained why.

        Regards.    Mel.



More information about the Python-list mailing list