can someone explain?
sismex01 at hebmex.com
sismex01 at hebmex.com
Mon Feb 17 11:49:08 EST 2003
> From: pawelp at softsystem.pl [mailto:pawelp at softsystem.pl]
> Sent: Monday, February 17, 2003 10:36 AM
>
> Hello everyone.
>
> I'm interested in Python and I've been reading some tutorials,
> documents, etc. but could't find an answer for a few questions.
> How can I pass an address (reference) of a variable to a function?
> I know that when passing a list, its reference is actually passed. I
> assume the same applies to class's instances. But what about other
> variables?
>
> def increase(val):
> val +=1
>
> k = 0
> increase(k)
> print k
>
> What should I do to make k equal 1 in that case? increase() changes
> only a copy, how to make it change passed variable?
>
> Why doesn't python let encapsulate attributes in classes? I can add
> attribute to class whenever I want. I don't see any reason why.
>
> And the last question.
> def f(a,L=[]):
> L.append(a)
> return L
>
> >>f(1)
> [1]
> >>f(2)
> [1,2]
> >>f(3)
> [1,2,3]
>
> L is a default argument so every time when a function f is invoked, L
> should be created and as a result f should return a list with only one
> value.
> Why doesn't it work that way?
>
> I really like Python, lists, dictionaries and tuples. I'm considering
> this language as a good tool for building gtk frontends but I'm really
> disapointed that the same logic from C++ or Java doesn't exist in
> Python.
> I hope someone can explain it to me.
> Thanks in advance.
>
> Cheers
> Pablo
>
> P.S. Please place pawelp at mail dot softsystem dot pl in CC field.
> Thanks a lot
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Greetings Pawelp,
You've just stumbled on some newbie issues regarding Python.
It's a pretty friendly language, but it's got it's gotchas,
regarding object references specifically.
When you pass objects (everything is an object, an int,
a string, a list, etc) to a function, you're already passing
a "pointer" to that object. The thing is, some object
classes are immutable: integers are immutable, so are
strings, and tuples; lists and dictionaries are mutable.
When you do a "val += 1", you're creating a new integer
object which has a value (val+1) and assigning it's
"address" (or it's reference, as it's known in this
group) to the name "val".
Remember: THIS IS NOT C++ NOR JAVA, so variable names
are just that, _N_A_M_E_S_. A name can refer to any object,
a reference is assigned to a name via the assignment statement.
NAMES do not have types, OBJECTS have types.
Regarding your second problem.
When you define:
> def f(a,L=[]):
> L.append(a)
> return L
You're creating a "function object" (everything is an object,
remember?), and this object takes two arguments, "a" and "L".
Furthermore, argument "L" has a default argument value which
is a list. THIS is where it gets wierd. The empty list
which is the default argument for "L" is compiled ONLY ONCE,
so, there is a single list object which is passed as default
argument for "L" every time you execute "f".
It's kinda like a "static" variable in C++, it's not accesible
like a global, but it stays put between invocations.
It's preferred to use the following idiom:
> def f(a,L=None):
> if L is None: L = []
> L.append(a)
> return L
Now, whenever "L" is None, a new list will be created and
assigned to "L", every time you execute "f".
Hope this helps a bit. :-)
-gustavo
More information about the Python-list
mailing list