By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

New variable?

P: n/a
What's the proper way to instantiate a new variable? x = ""?
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Jun 3, 8:40*pm, tmallen <thomasmal...@gmail.comwrote:
What's the proper way to instantiate a new variable? x = ""?
You don't need to pre-declare your variables. Just assign them as you
need them and they will take the correct type.
Jun 27 '08 #2

P: n/a
On Jun 3, 3:03 pm, Chris <cwi...@gmail.comwrote:
On Jun 3, 8:40 pm, tmallen <thomasmal...@gmail.comwrote:
What's the proper way to instantiate a new variable? x = ""?

You don't need to pre-declare your variables. Just assign them as you
need them and they will take the correct type.
unless I'm using the += or a similar operator, right? That doesn't
seem to instantiate a variable.
Jun 27 '08 #3

P: n/a
On Tue, Jun 3, 2008 at 3:16 PM, tmallen <th**********@gmail.comwrote:
On Jun 3, 3:03 pm, Chris <cwi...@gmail.comwrote:
>On Jun 3, 8:40 pm, tmallen <thomasmal...@gmail.comwrote:
What's the proper way to instantiate a new variable? x = ""?

You don't need to pre-declare your variables. Just assign them as you
need them and they will take the correct type.

unless I'm using the += or a similar operator, right? That doesn't
seem to instantiate a variable.
Right... because you don't have anything to increment or append to. I
guess this also comes up in the case of something like lists or
dictionaries you want to uniformly create in a loop. I guess you
could call it instantiating, but really it's more like going ahead and
assigning to them as Chris mentioned and you're just starting them
with a default value. Assuming you're working with strings, x=""
should work just fine in that case. Lists, x=[], dictionaries, x={},
integers, probably x=1 or x=0...
Jun 27 '08 #4

P: n/a
-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of tmallen
Sent: Tuesday, June 03, 2008 2:41 PM
To: py*********@python.org
Subject: New variable?

What's the proper way to instantiate a new variable? x = ""?
I've always used
X = None
in those cases where I need to pre-declare a variable to set scope.
*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA623
Jun 27 '08 #5

P: n/a
On Jun 3, 9:34*pm, "Dan Upton" <up...@virginia.eduwrote:
On Tue, Jun 3, 2008 at 3:16 PM, tmallen <thomasmal...@gmail.comwrote:
On Jun 3, 3:03 pm, Chris <cwi...@gmail.comwrote:
On Jun 3, 8:40 pm, tmallen <thomasmal...@gmail.comwrote:
What's the proper way to instantiate a new variable? x = ""?
You don't need to pre-declare your variables. *Just assign them as you
need them and they will take the correct type.
unless I'm using the += or a similar operator, right? That doesn't
seem to instantiate a variable.

Right... because you don't have anything to increment or append to. *I
guess this also comes up in the case of something like lists or
dictionaries you want to uniformly create in a loop. *I guess you
could call it instantiating, but really it's more like going ahead and
assigning to them as Chris mentioned and you're just starting them
with a default value. *Assuming you're working with strings, x=""
should work just fine in that case. *Lists, x=[], dictionaries, x={},
integers, probably x=1 or x=0...
You can always use the conversion functions if you want to be explicit
str(), int(), list(), dict(), set() etc
Jun 27 '08 #6

P: n/a
Lie
On Jun 4, 1:40*am, tmallen <thomasmal...@gmail.comwrote:
What's the proper way to instantiate a new variable? x = ""?
You don't need to. The reason why you need to "declare" variable when
doing something like a += 1 is because this is actually a shorthand
for a = a + 1 (unless you override __radd__), the a on the right-hand
side is not yet assigned to any objects (remember that python use the
"name tag" model instead of "variable" model).
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.