Python variables are bound to types when used?

Fredrik Lundh fredrik at pythonware.com
Wed Oct 19 16:04:09 EDT 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?

no.  variables are bound to objects, and objects have types.

> Eg.
> x = 10 # makes it an INT

no.  that binds the name "x" to an integer object.

> whereas
> x = "hello" # makes it a string

no.  that (re)binds the name "x" to a string object.

> I take it, the parameters to a function (in the above example "l2") are
> bound in the definition, rather than as invoked.

not sure what you're saying here.  when you call a function, each parameter
is bound to the object represented by the corresponding argument.

> So, if I use "l2" thus:
>
> if (l2): # only then does it make it a boolean?

no. that queries the object to see if it's "true".

> and if I did,
>
> if (l2 = "hello"): # would it become string?

no.  that's a syntax error; if you fix that, it queries the object to see
how compares itself to the given string object.

> Elucidate please.

reset your brain:

    http://effbot.org/zone/python-objects.htm

</F>






More information about the Python-list mailing list