can someone explain?
Mongryong at sympatico.ca
Tue Feb 18 07:19:33 CET 2003
On Mon, 2003-02-17 at 11:35, Pablo wrote:
> def increase(val):
> val +=1
As other's have explained, simple types like 'ints' are
passed-by-value. You'll need a wrapper class for pass as reference.
> 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.
To enforce true OO encapsulate would probably mean more work for the
Python VM -> hence, that makes Python slower. Yes, it's true that you
can add attributes to a class whenever you want to - however, I believe
this feature had to be added to support inheritance. The base class
initializes 'self' with attributes, then the extended class may add new
attributes to 'self'. Now, if the Python VM were to forbid one from
being able to add new attributes to 'self' outside of the '__init__'
method, then the Python VM would have to do checks everytime 'self' is
assigned a value.
Python in a way is very much like C++. C++ has all these OO features,
but it provides a 'back door' to support 'C' so that it can easily be
integrated with legacy 'C' code and optimized at the 'hardware' level.
More importantly, C++ provides these escape routes for situations where
there is no other 'clean (OO) way' to solve a problem - so, you just
'hack it' with a 'C' solution. Python's no different. In fact, because
Java is so strict, it makes it difficult for non-OO programmers to use
If you're a OO purist, Python has the 'essential' features for you. If
you're a procedural type programmer, Python has all the features you
need as well.
> And the last question.
> def f(a,L=):
> return L
> 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
> Why doesn't it work that way?
Default parameters are initialized at import time. Don't know why it
was done this way - maybe to support 'static method variables'?
More information about the Python-list