Your question seems to be, "How is this a type declaration?" The answer is, it isn't a type declaration. Names in Python have no type associated with them. Names refer to values, and values have a type, determined at runtime.
When Python executes a = float()
, it looks up the name float
, and finds it in the builtins, it's a function. It calls that function with no arguments. The return value is a float object. The name a
is then made to refer to that object. That's all it does. Before it's executed this line of code, Python has no idea what a
will become, and it has no idea that floats will be involved.
Python is dynamic, so your line of code could have been in this program:
def float():
return "I'm not a float!"
a = float()
Now when a = float()
is executed, the builtin has nothing to do with it, and there are no floats anywhere, and a
refers to a string.
For more on names and values, see Facts and Myths about Python Names and Values.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…