variable & scoping question.

Cornelius Keller cornelius.keller at googlemail.com
Mon Aug 10 11:07:28 EDT 2009


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







More information about the Python-list mailing list