can someone explain?

Pablo pawelp at softsystem.pl
Mon Feb 17 11:35:51 EST 2003


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




More information about the Python-list mailing list