New variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tmallen

    New variable?

    What's the proper way to instantiate a new variable? x = ""?
  • Chris

    #2
    Re: New variable?

    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.

    Comment

    • tmallen

      #3
      Re: New variable?

      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.

      Comment

      • Dan Upton

        #4
        Re: New variable?

        On Tue, Jun 3, 2008 at 3:16 PM, tmallen <thomasmallen@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...

        Comment

        • Reedick, Andrew

          #5
          RE: New variable?

          -----Original Message-----
          From: 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 = ""?
          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


          Comment

          • Chris

            #6
            Re: New variable?

            On Jun 3, 9:34 pm, "Dan Upton" <up...@virginia .eduwrote:
            On 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...
            You can always use the conversion functions if you want to be explicit
            str(), int(), list(), dict(), set() etc

            Comment

            • Lie

              #7
              Re: New variable?

              On Jun 4, 1:40 am, tmallen <thomasmal...@g mail.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).

              Comment

              Working...