Python variables are bound to types when used?
kent37 at tds.net
Wed Oct 19 22:13:25 CEST 2005
pranab_bajpai at yahoo.com wrote:
> So I want to define a method that takes a "boolean" in a module, eg.
> def getDBName(l2):
> Now, in Python variables are bound to types when used, right?
> x = 10 # makes it an INT
> x = "hello" # makes it a string
You don't have it quite right. In each case, the name 'x' is bound to a value. The type is a property of the value, not of the name.
> I take it, the parameters to a function (in the above example "l2") are
> bound in the definition, rather than as invoked.
No, the parameter values are bound to the parameter names at the time of call. Again, it is the value that is typed, not the name.
> So, if I use "l2" thus:
> if (l2): # only then does it make it a boolean?
No, this doesn't change the type of the value bound to l2, it just evaluates the value as True or False according to the rules here:
> and if I did,
> if (l2 = "hello"): # would it become string?
No; assuming you mean to compare l2 == "hello", this will call a special method of the object bound to l2, passing "hello" as a parameter. The result will be evaluated as True or False. The actual method called on l2 may be __cmp__ or __eq__.
> and what if I never used it in the definition body?
> Elucidate please.
More information about the Python-list