What's the proper way to instantiate a new variable? x = ""?
New variable?
Collapse
This topic is closed.
X
X
-
tmallenTags: None -
Chris
Re: New variable?
On Jun 3, 8:40 pm, tmallen <thomasmal...@g mail.comwrote:You don't need to pre-declare your variables. Just assign them as youWhat's the proper way to instantiate a new variable? x = ""?
need them and they will take the correct type.
-
tmallen
Re: New variable?
On Jun 3, 3:03 pm, Chris <cwi...@gmail.c omwrote:unless I'm using the += or a similar operator, right? That doesn'tOn Jun 3, 8:40 pm, tmallen <thomasmal...@g mail.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.
seem to instantiate a variable.
Comment
-
Dan Upton
Re: New variable?
On Tue, Jun 3, 2008 at 3:16 PM, tmallen <thomasmallen@g mail.comwrote:Right... because you don't have anything to increment or append to. IOn Jun 3, 3:03 pm, Chris <cwi...@gmail.c omwrote:>>On Jun 3, 8:40 pm, tmallen <thomasmal...@g mail.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.
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...
Comment
-
Reedick, Andrew
RE: New variable?
-----Original Message-----I've always usedFrom: python-list-bounces+jr9445= att.com@python. org [mailto:python-
list-bounces+jr9445= att.com@python. org] On Behalf Of tmallen
Sent: Tuesday, June 03, 2008 2:41 PM
To: python-list@python.org
Subject: New variable?
What's the proper way to instantiate a new variable? x = ""?
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
Comment
-
Chris
Re: New variable?
On Jun 3, 9:34 pm, "Dan Upton" <up...@virginia .eduwrote:You can always use the conversion functions if you want to be explicitOn Tue, Jun 3, 2008 at 3:16 PM, tmallen <thomasmal...@g mail.comwrote:>On Jun 3, 3:03 pm, Chris <cwi...@gmail.c omwrote:On Jun 3, 8:40 pm, tmallen <thomasmal...@g mail.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...
str(), int(), list(), dict(), set() etc
Comment
-
Lie
Re: New variable?
On Jun 4, 1:40 am, tmallen <thomasmal...@g mail.comwrote:You don't need to. The reason why you need to "declare" variable whenWhat's the proper way to instantiate a new variable? x = ""?
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).
Comment
Comment