New variable?

Dan Upton upton at virginia.edu
Tue Jun 3 15:34:58 EDT 2008


On Tue, Jun 3, 2008 at 3:16 PM, tmallen <thomasmallen at gmail.com> wrote:
> On Jun 3, 3:03 pm, Chris <cwi... at gmail.com> wrote:
>> On Jun 3, 8:40 pm, tmallen <thomasmal... at gmail.com> wrote:
>>
>> > 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...



More information about the Python-list mailing list