[Tutor] pointers for python?

Kalle Svensson kalle at lysator.liu.se
Sat Oct 4 13:15:38 EDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[Gonçalo Rodrigues on modifying function parameters]
> P. Brown in another reply gave you another answer involving the
> global keyword -- but it's ugly and unpythonic IMHO and it only
> works for global names (e.g. module-level). Right now I can't
> remember of any other solution -- and I suspect any such would
> involve some dangerous hackery.

Using some kind of mutable object to wrap the argument is one way to
get (most of) the desired behaviour.  Example:

>>> def inc(x):
...     x[0] += 1
... 
>>> a = [0]
>>> inc(a)
>>> a[0]
1

Something similar but better looking could probably be created by
defining a class.  Remember that the assignment operator can't be
used, though.  In code such as

  def set2(x):
      x[0] = 2

what looks as a variable assignment is really an item assignment.

Peace,
  Kalle
- -- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE/fwAadNeA1787sd0RAss7AJ9X/ZGCakZys9BvgNKMUGxI1wvu+ACdHVxT
2zFcNFTlpRZh47RTBRuERAA=
=L2LE
-----END PGP SIGNATURE-----



More information about the Tutor mailing list