"always passes by reference"

Donald O'Donnell donod at home.com
Tue Jul 25 23:29:51 EDT 2000


Don't know if it's good or bad as long as you understand how it behaves
and program accordingly.  The glitch is that Python treats built-in
types and class instances differently, as far as argument passing. 
Python's class instances (objects) behave more like Perl's:

    class cc:
        pass

    def f(a):
        a.v = 42

    c = cc()
    c.v = 1
    f(c)
    print c.v               # PRINTS 42

Don O'Donnell 

P.S. -
I've been studying and using Python for a couple weeks now - rewriting a
major main-frame app in Python.  It's the greatest invention since
chocolate chip cookies.  It's been around for 10 years, so how come I
haven't heard of it till now?  Either I'm out of touch (probably), or
Python needs more promoting and marketing.

It happened by accident, just happened to notice David Beasley's book on
a table in B&N one day, started leafing through it, and gradually
realized it was the answer to my prayers.  The perfect tool for porting
an interactive main-frame data analysis package (needing GUI and
scripting) to a wintel platform.  Thank you Guido and everyone else who
made my job possible and brought fun back to programming. (I love being
able to get inside and play with the machinery.  It's a lot like Forth
in that respect.)


"(Greg Weeks)" wrote:
> 
> (Greg Weeks) (weeks at golden.dtc.hp.com) wrote:
> : I prefer the old way of speaking.  With the new way of speaking, both
> : Python and Perl are "pass by reference", but Perl is somehow *more* so.
> 
> If it isn't clear what I mean, here's some Perl:
> 
>     sub f
>     {
>         $_[0] = 42;
>     }
> 
>     $x = 1;
>     f($x);
>     print "$x\n";               # PRINTS 42
> 
> and the corresponding Python:
> 
>     def f(a):
>         a = 42
> 
>     x = 1
>     f(x)
>     print x                     # PRINTS 1
> 
> Good Python!  Bad Perl!
> 
> Greg



More information about the Python-list mailing list