Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python variables are bound to types when used?

Reply
Thread Tools

Python variables are bound to types when used?

 
 
pranab_bajpai@yahoo.com
Guest
Posts: n/a
 
      10-19-2005
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?

Eg.
x = 10 # makes it an INT
whereas
x = "hello" # makes it a string

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

So, if I use "l2" thus:

if (l2): # only then does it make it a boolean?

and if I did,

if (l2 = "hello"): # would it become string?

and what if I never used it in the definition body?

Elucidate please.

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-19-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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>



 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      10-19-2005
On 2005-10-19, (E-Mail Removed) <(E-Mail Removed)> 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?


Python doesn't have variables.

Python has objects of various types. You can bind 0 or more
names an object.

> Eg.
> x = 10 # makes it an INT
> whereas
> x = "hello" # makes it a string


No, not really. There is no "it" that's becoming different
types.

x = 10

creates an integer object with the value 10 and binds the name
"x" to it.

x = "hello"

creates a string object containing the value "hello" and then
unbinds the name "x" from the integer object and re-binds it to
the string object. [At that point, the integer object _may_
get deleted if it's not being used any longer (it may have had
multiple names).]

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


Not sure I understand the question. The function you defined
accepts a single object as a parameter. When the function is
invoked, that object has the local name "l2" bound to it

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


That doesn't affect the type of the object with the name "l2"
at all. It checks to see if l2 has a false value or not.
Examples of basic objects with false values are an iteger 0, a
floating point 0.0, an empty string "", an empty list [], an
empty tuple (), or an empty dictionary {}.

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


That's not legal python. I presume you mean

if l2 == "hello":

The expression

l2 == "hello"

checks to see if the object with the name "l2" is a string with
the value "hello". If the object you passed to the function is
a string object with the value "hello", that experession will
be true. The expression will be false for any object that
isn't a string, and false for any string object that doesn't
have the value "hello".

> and what if I never used it in the definition body?


Then it doesn't get used.

--
Grant Edwards grante Yow! I just forgot my
at whole philosophy of life!!!
visi.com
 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      10-19-2005
On 19 Oct 2005 12:51:02 -0700, (E-Mail Removed)
<(E-Mail Removed)> 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?


Python doesn't really have variables as such. It has objects, which
are typed, and names, which are not.

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


The name 'x' is now bound to an int.

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


Now it's bound to a string.

> I take it, the parameters to a function (in the above example "l2") are
> bound in the definition, rather than as invoked.
>
> So, if I use "l2" thus:
>
> if (l2): # only then does it make it a boolean?
>
> and if I did,
>
> if (l2 = "hello"): # would it become string?
>
> and what if I never used it in the definition body?


Now you've lost me. Probably my problem - serves me right for posting
from the pub.

> Elucidate please.


I'll allow a true Python Zen master to do that -
<http://effbot.org/zone/python-objects.htm>.

--
Cheers,
Simon B,
(E-Mail Removed),
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      10-19-2005
On 2005-10-19, Grant Edwards <(E-Mail Removed)> wrote:

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

>
> That doesn't affect the type of the object with the name "l2"
> at all. It checks to see if l2 has a false value or not.
> Examples of basic objects with false values are an iteger 0, a
> floating point 0.0, an empty string "", an empty list [], an
> empty tuple (), or an empty dictionary {}.


Oh, and a bool False, obviously.

--
Grant Edwards grante Yow! It's so OBVIOUS!!
at
visi.com
 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      10-19-2005
(E-Mail Removed) 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?
>
> Eg.
> x = 10 # makes it an INT
> whereas
> 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:
http://docs.python.org/ref/Booleans.html#Booleans
>
> 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__.
http://docs.python.org/ref/customization.html

Kent

>
> and what if I never used it in the definition body?
>
> Elucidate please.
>

 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      10-19-2005
In <(E-Mail Removed) .com>, pranab_bajpai
wrote:

> So I want to define a method that takes a "boolean" in a module, eg.
>
> def getDBName(l2):
> ...


Is that a 12 or l2?

> Now, in Python variables are bound to types when used, right?
>
> Eg.
> x = 10 # makes it an INT
> whereas
> x = "hello" # makes it a string


No, think of `x` as a *name*. The name has *no* type. The objects you
bind to that name have a type. So 10 is an int and "hello" is a string.

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


If you invoke the function then the parameter is bound to an object. This
object has a type.

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


Here `l2` is treated as a boolean. If it is an integer then 0 is false,
everything else is true, if it is a list, dictionary or string then an
"empty" object is false, everything else is true. Otherwise it depends on
the existence and return value of either a `__nonzero__()` or the
`__len__()` method. See the docs for details.

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


It would become a syntax error. No assignement allowed there.

> and what if I never used it in the definition body?


Again: The objects have types, the names not. A string that is never used
remains a string.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
sjdevnull@yahoo.com
Guest
Posts: n/a
 
      10-19-2005
Fredrik Lundh wrote:
> reset your brain:
>
> http://effbot.org/zone/python-objects.htm


Neat link.

Can you expand on this:

> a type (returned by type(x))

....
> You cannot change the type.


Especially what's going on here:

>>> class a(object):

.... pass
....
>>> class b(a):

.... pass
....
>>> x=b()
>>> x.attr=1
>>> type(x)

<class '__main__.b'>
>>> type(y)

<class '__main__.b'>
>>> x.__class__=a
>>> type(x)

<class '__main__.a'>
>>> x.attr

1

It looks to me like x is still referencing the same object (and still
has the "attr" attribute) but its type has changed. Is that not right?

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-19-2005
On Wed, 19 Oct 2005 20:06:53 +0000, Grant Edwards wrote:

>> Now, in Python variables are bound to types when used, right?

>
> Python doesn't have variables.
>
> Python has objects of various types. You can bind 0 or more
> names an object.


I frequently use "variable" as a synonym for "name" when talking about
Python code. Do people think I am wrong to do so?


--
Steven.

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      10-19-2005
Steven D'Aprano wrote:
> On Wed, 19 Oct 2005 20:06:53 +0000, Grant Edwards wrote:
>
>>>Now, in Python variables are bound to types when used, right?

>>
>>Python doesn't have variables.
>>
>>Python has objects of various types. You can bind 0 or more
>>names an object.

>
> I frequently use "variable" as a synonym for "name" when talking about
> Python code. Do people think I am wrong to do so?


It depends on context and audience. If you're talking about Python code
with other relatively experienced Pythonistas, no I don't think it's
wrong. When you're talking about Python versus other languages where
"variable" corresponds with "a possibly typed memory location", or
you're talking with someone who is coming from such a language, then
it's probably best to talk about names and objects. Such pedantry is
useful in the latter cases but not so much in the former.

--
Robert Kern
(E-Mail Removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lower bound & Upper bound sunil panda Java 9 10-07-2008 08:32 PM
Passing Bound Object value to another Bound Object Phillip Vong ASP .Net 0 07-27-2006 10:54 PM
How to represent the data in a not bound control via bound control Mario Krsnic ASP .Net 0 06-23-2006 07:38 AM
Getting lower-bound and upper-bound of strings input Rhiner Dan C++ 1 03-27-2005 02:03 AM
Boost + Python C/API: Mixing python return types with boost return types Steve Knight Python 2 10-10-2003 10:11 AM



Advertisments