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

New variable?

What's the proper way to instantiate a new variable? x = ""?
Jun 27 '08 #1
6 2273
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.