Declaring variables

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

    #1

    Declaring variables

    I looked in the language but did not find a switch for requiring
    variables to be declared before use.

    Is such an option available?

    Thanks,

    jvh

  • Grant Edwards

    #2
    Re: Declaring variables

    On 2007-05-16, HMS Surprise <john@datavoice int.comwrote:
    I looked in the language but did not find a switch for requiring
    variables to be declared before use.
    Still trying to write Pascal, eh? ;)
    Is such an option available?
    No.

    However, there are utilities to "proofread" your code should
    you have a desire for that:




    --
    Grant Edwards grante Yow! What I need is a
    at MATURE RELATIONSHIP with a
    visi.com FLOPPY DISK ...

    Comment

    • HMS Surprise

      #3
      Re: Declaring variables


      No haven't had to endure Pascal. Mostly C/C++, Tcl, and assembler. Oh
      yeah, and a (thankfully) short stint of Ada.

      But I glad to hear of the proofing tools. Working a lot of data parsed
      from web pages and the developer there a different naming convention
      from what I am accustomed so sometimes I introduce a new variable
      unintentionally .


      Thanks,

      jvh

      Comment

      • Matimus

        #4
        Re: Declaring variables

        On May 16, 9:57 am, HMS Surprise <j...@datavoice int.comwrote:
        I looked in the language but did not find a switch for requiring
        variables to be declared before use.
        >
        Is such an option available?
        >
        Thanks,
        >
        jvh
        You do have to declare a variable before use. You do so by assigning
        it a value. You can't use a variable before it has been assigned. In
        some ways this is less ambiguous than even C where you can declare a
        variable without assigning a value. Also note that this caries the
        type information, since the variable is of whatever type was assigned
        to it. The only thing it doesn't do is give a unique flag that says
        "hey this is where I'm declared", although I suppose you could do that
        with a comment.

        Matt

        Comment

        • Steve Holden

          #5
          Re: Declaring variables

          Matimus wrote:
          On May 16, 9:57 am, HMS Surprise <j...@datavoice int.comwrote:
          >I looked in the language but did not find a switch for requiring
          >variables to be declared before use.
          >>
          >Is such an option available?
          >>
          >Thanks,
          >>
          >jvh
          >
          You do have to declare a variable before use. You do so by assigning
          it a value. You can't use a variable before it has been assigned. In
          some ways this is less ambiguous than even C where you can declare a
          variable without assigning a value. Also note that this caries the
          type information, since the variable is of whatever type was assigned
          to it. The only thing it doesn't do is give a unique flag that says
          "hey this is where I'm declared", although I suppose you could do that
          with a comment.
          >
          Strictly, the variable has no type at all (and strictly your "variables"
          are actually names bound to values in a namespace, and it's the values
          that are typed).

          We shouldn't ignore the fact that declarations unambiguously say "the
          programmer intends to use such-and-such a name for a value of a specific
          type". Contrast this with a Python program where one path makes an
          assignment (binding) to a name while another path doesn't, resulting in
          a later NameError.

          regards
          Steve
          --
          Steve Holden +1 571 484 6266 +1 800 494 3119
          Holden Web LLC/Ltd http://www.holdenweb.com
          Skype: holdenweb http://del.icio.us/steve.holden
          ------------------ Asciimercial ---------------------
          Get on the web: Blog, lens and tag your way to fame!!
          holdenweb.blogs pot.com squidoo.com/pythonology
          tagged items: del.icio.us/steve.holden/python
          All these services currently offer free registration!
          -------------- Thank You for Reading ----------------

          Comment

          • HMS Surprise

            #6
            Re: Declaring variables

            On May 16, 6:48 pm, Matimus <mccre...@gmail .comwrote:
            On May 16, 9:57 am, HMS Surprise <j...@datavoice int.comwrote:
            >
            I looked in the language but did not find a switch for requiring
            variables to be declared before use.
            >
            Is such an option available?
            >
            Thanks,
            >
            jvh
            >
            You do have to declare a variable before use. You do so by assigning
            it a value. You can't use a variable before it has been assigned.
            Yes this is where the problem arises. This is a gross
            oversimplificat ion , but is where I typically find problems.

            jh


            #~~~~~~~~~~~~~~ ~~~~~~~~
            createdIncident Id = 0
            ..
            ..
            ..
            #attempt to change varialbe
            createdIncident ID = 1
            ..
            ..
            ..
            if createdIncident Id == 1:
            ...

            Comment

            • Gregor Horvath

              #7
              Re: Declaring variables

              HMS Surprise schrieb:
              >
              #~~~~~~~~~~~~~~ ~~~~~~~~
              createdIncident Id = 0
              .
              .
              .
              #attempt to change varialbe
              createdIncident ID = 1
              .
              .
              .
              if createdIncident Id == 1:
              ...
              >
              test.py is your code above

              $ pychecker -v test.py
              Processing test...

              Warnings...

              test.py:7: Variable (createdInciden tID) not used


              Gregor

              Comment

              • Grant Edwards

                #8
                Re: Declaring variables

                On 2007-05-16, HMS Surprise <john@datavoice int.comwrote:
                No haven't had to endure Pascal. Mostly C/C++, Tcl, and assembler.
                I must have you mixed up with somebody else who recently
                mentioned having Pascal as their first real language.

                --
                Grant Edwards grante Yow! It's OKAY -- I'm an
                at INTELLECTUAL, too.
                visi.com

                Comment

                • HMS Surprise

                  #9
                  Re: Declaring variables

                  On May 17, 9:34 am, Grant Edwards <gra...@visi.co mwrote:
                  On 2007-05-16, HMS Surprise <j...@datavoice int.comwrote:
                  >
                  No haven't had to endure Pascal. Mostly C/C++, Tcl, and assembler.
                  >
                  I must have you mixed up with somebody else who recently
                  mentioned having Pascal as their first real language.
                  >
                  --
                  Grant Edwards grante Yow! It's OKAY -- I'm an
                  at INTELLECTUAL, too.
                  visi.com
                  That's OK. I am easily mixed up.

                  Comment

                  Working...