Different methods with same name but different signature?

7stud bbxx789_05ss at yahoo.com
Thu May 24 14:27:56 EDT 2007


In python, every(?) variable name and its value is stored in a dict
somewhere, and when you try to access the name, it is looked up in the
dict.  So if you write:

def f(x):
    print x
def f(x, y):
    print x,y

when those lines are first parsed as your file loads, somewhere this
happens:

somedict = {}
somedict["f"] = fx
somedict["f"] = fxy

which has the same effect as:

d = {}
d["a"] = 10
d["a"] = "hello"
print d["a"]

--output:--
"hello"


So the second value for "f" overwrites the first value.  Then when you
call f(...), the name "f" is looked up in the dict, and the
corresponding value is returned.  In this case, that would be a
reference to the second function f(x,y).




More information about the Python-list mailing list