473,289 Members | 1,875 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,289 software developers and data experts.

New variable?

What's the proper way to instantiate a new variable? x = ""?
Jun 27 '08 #1
6 2269
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
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
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
-----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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

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.