can someone explain?

Nagy László nagylzs at freemail.hu
Mon Feb 17 12:32:16 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?
>
You are thinking in C where a variable has an address where data begins, 
but you are
able to get the value of the variable and the address of it too. In 
Python, all variables
are references to objects. Even when you do something like this:

val = 1

val will be a reference to the object 1. I will use a mixture of the 
terminology of
C and Python here, please forgive me. When you call a function, all actual
paramerters are passed by value but they are all references (if you are 
watching
this as a C programmer).

>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
>
Now you can see that when you cal increase(k) the value of val will be a
reference to the object 1. This reference is passed and this reference will
be assigned to the formal parameter "val" in your increase function. After
that when you do this:

val += 1

val will be 2,  but 2 is another object.

>What should I do to make k equal 1 in that case? increase() changes
>only a copy, how to make it change passed variable?
>
I'm affraid you will have to write a function to do this.

def increase(val):
    return val+1

val = 1
val = increase(val).

The key is that 1 is an immutable object. You cannot change the value of 
an immutable
object but you can create other immutable objects using your original 
one. (There is
a detailed description of immutable and mutable objects in the Python 
documentation.)

>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.
>
I think it does encapsulate attributes but you can add and delete class 
attributes at runtime.
It is more dynamic but you are not enforced to use this feature. For 
example, you can do this:

class A:
    pass

a = A()
A.a = 8
b = A()
print b.a    # This works
print a.a    # This works too!

>And the last question.
>def f(a,L=[]):
>  L.append(a)
>  return L
>
In this example, please take a note on the fact that  L=[] is the part 
of the function definition.
In Python, there are no dynamically evaluated default arguments. The 
L=[] statement is evaluated
only once (when you create the function using 'def'). So the default 
value of the parameter L will
be []. But this is a list! When you execute L.append(a), this object 
will change. Rememver, lists are
mutable objects. The default argument will be the same object anytime 
you call the function, but it
will change every time you call your function.

Just try this, and all everything will be clear:

def f(a,L=[]):  # This will create the default parameter value
  L.append(a)
  return L


print f(1)  # [1]
print f(2)  # [1,2]
print f(3)  # [1,2,3]

a = f(4)   # a is now [1,2,3,4]
a.remove(1)
a.remove(2)
a.remove(3)
a.remove(4)    # a is now [], and this is still the same object!

print f(10)      # This will print [10]!!!


I hope this helps.

  Laci 1.0







More information about the Python-list mailing list