[Tutor] Confounded again
alan.gauld@bt.com
alan.gauld@bt.com
Sat Nov 30 14:11:50 2002
I posted a reply saying you couldn't call a function like:
f(i=5)
And this is true. But apparently within a class method you can!
Does anyone know how this works? Here is a session showing what
I did...
Python 2.2.1 (#34, Apr 9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo (i):
... print i
...
>>> foo(j = 7)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: foo() got an unexpected keyword argument 'j'
So that failed, as I expected.
>>> class C:
... def __init__(s,i):
... print i
...
>>> class D(C):
... def __init__(s):
... C.__init__(s, i = 7)
...
Now I expected an error here, but if not then definitely when
I tried calling it as in:
>>> d = D()
7
>>>
But it worked! How?
And just to check that it wasn't the nesting that allowed it,
I tried it again inside a function:
>>> def bar():
... foo(n=9)
...
>>> bar()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in bar
TypeError: foo() got an unexpected keyword argument 'n'
>>>
Yep, still an error.
I'm confused (again),
Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld