473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Python variables are bound to types when used?

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.

Oct 19 '05 #1
15 2279
pr***********@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>

Oct 19 '05 #2
On 2005-10-19, pr***********@yahoo.com <pr***********@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?
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
Oct 19 '05 #3
On 19 Oct 2005 12:51:02 -0700, pr***********@yahoo.com
<pr***********@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?
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,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Oct 19 '05 #4
On 2005-10-19, Grant Edwards <gr****@visi.com> 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
Oct 19 '05 #5
pr***********@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?

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.

Oct 19 '05 #6
In <11**********************@g49g2000cwa.googlegroups .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
Oct 19 '05 #7
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?

Oct 19 '05 #8
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.

Oct 19 '05 #9
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
rk***@ucsd.edu

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

Oct 19 '05 #10
In article <pa****************************@REMOVETHIScyber.co m.au>,
Steven D'Aprano <st***@REMOVETHIScyber.com.au> wrote:
On Wed, 19 Oct 2005 20:06:53 +0000, Grant Edwards wrote:

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?


Wrong? No. Bad idea? Possibly, depending on context. I'm often using
"variable" in casual conversation -- it's hard to break the habit of
thirty years of programming! But I try very hard to stick with "name"
when I'm doing formal instruction.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur." --Red Adair
Oct 20 '05 #11
sj*******@yahoo.com wrote:
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?


So what? That the flexibility of python allows for such a hack has
nothing to do with x being totally unaware of the type of the object it
points to.

You only access values by names pointing/refering to them. If they are
mutable, you can mutate them. If not (like strings, ints and floats),
you have to rebind the name with a new value. But none of these changes
what x is - and what not. Its a name, pointing to a value, and knows
nothing about what it is actually bound to.
Regards,

Diez
Oct 21 '05 #12

Diez B. Roggisch wrote:
sj*******@yahoo.com wrote:
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.

.... So what? That the flexibility of python allows for such a hack has
nothing to do with x being totally unaware of the type of the object it
points to.


I'm still not understanding, maybe my question was unclear (and was
related to the writeup that was linked, I wasn't saying anything about
the relationships of names and objects).

I'm wondering what is meant by "you cannot change the type", and
whether I'm misunderstanding what is going on in my example when I say
"x.__class__=a". It looks to me like the type of the object that "x"
is referencing changes, but the URL implies that something else might
actually be happening. I'm not understanding the distinction.

Oct 21 '05 #13
sj*******@yahoo.com 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.
the page was written before the "type/class unification" in Python 2.2,
at a time where the word "type" had a stricter meaning (referring to C-
level types, not user-level classes).

in CPython 2.2 and later, you can in fact change the type under some
circumstances, as long as the internal structure (the "C-level type") is
identical. the types involved must (to quote the checkin messages):

- have the same basic size
- have the same item size
- have the same dict offset
- have the same weaklist offset
- have the same GC flag bit
- have a common base that is the same except for maybe the
dict and weaklist (which may have been added separately at
the same offsets in both types)
- both be heap types

this basically limits the feature to classes defined at the Python level
(just like before the unification); most attempts to use arbitrary types
will fail. e.g.
x.__class__ = dict Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __class__ assignment: only for heap types
class c(list): .... pass
.... x.__class__ = c

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __class__ assignment: 'a' object layout differs from 'c'

I suppose it's time to add a footnote to the objects page...

</F>

Oct 22 '05 #14
On Sat, 22 Oct 2005, Fredrik Lundh wrote:
sj*******@yahoo.com wrote:
reset your brain:

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


Is it really a good idea to say that objects have names? Isn't it cleaner
to describe objects without any reference to names or variables or
whatnot, then introduce names, namespaces and references as tools for
working with objects? You go on to explain this clearly, but it's a bit of
a confusing way to start!

tom

PS Sorry to be following up to a message other than the one i'm actually
replying to - the original, i'm afraid, is an ex-message.

--
It is a laborious madness, and an impoverishing one, the madness of
composing vast books. -- Jorge Luis Borges
Oct 23 '05 #15
Fredrik Lundh wrote:
the page was written before the "type/class unification" in Python 2.2,
at a time where the word "type" had a stricter meaning (referring to C-
level types, not user-level classes).


Gotcha. Thanks.

That writeup is definitely on my required reading list for new Python
programmers.

Oct 24 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
4
by: Michael Yanowitz | last post by:
I am still new to Python but have used it for the last 2+ months. One thing I'm still not used to is that functions parameters can't change as expected. For example in C, I can have status =...
29
by: Gerald | last post by:
Hi ,Im a BSc4 Maths/Computer Science student.Unfortunately my curriculum did not include Python programming yet I see many vacancies for Python developers.I studied programming Pascal,C++ and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.