New variable?

C

Chris

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.
 
T

tmallen

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.
 
D

Dan Upton

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...
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of tmallen
Sent: Tuesday, June 03, 2008 2:41 PM
To: (e-mail address removed)
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
 
C

Chris

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
 
L

Lie

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top