variable & scoping question.

2009-08-10 Thread Cornelius Keller
Hi,

I'm a quite fresh python programmer, (6 Month python experience).
Today I found something I absolotly don'nt understand:

given the following function:

def test_effect(class_id=None,class_ids=[]):
if class_id is not None:
if class_id not in class_ids:
class_ids.append(int(class_id))

print class_ids

I observe that the class_ids array is growing when it is called with
different class id's.

I expected class_ids to be [] if the keyword argument is not set, but
it seems to beahve like a static variable if not set.

example:


In [3]: test.test_effect(class_id=1)
[1]

In [4]: test.test_effect(class_id=1)
[1]

In [5]: test.test_effect(class_id=2)
[1, 2]

In [6]: test.test_effect(class_id=2)
[1, 2]

In [7]: test.test_effect(class_id=3)
[1, 2, 3]

Is this an intended python beahviour?

sincerly
- Cornelius




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: variable & scoping question.

2009-08-10 Thread Cornelius Keller
On 10 Aug., 17:12, "Diez B. Roggisch"  wrote:
> Cornelius Keller wrote:
[snip]
>
> http://effbot.org/zone/default-values.htm
>
> Diez

Ok thank you.
I' understand now why.
I still think this is very confusing, because default values don't
behave like most people would expect without reading the docs.

- Cornelius
-- 
http://mail.python.org/mailman/listinfo/python-list