explicit variable declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luis Solís

    explicit variable declaration

    Hi
    It is possible to declare some variables as int, long... ? , or something
    like visual basic option explicit.
    In some situations could be usefull.
    Thanks


  • Michel Claveau/Hamster

    #2
    Re: explicit variable declaration

    Hi !

    "Variable" is a bad word - beurk ! - In Python there are ref-on-objects ;
    it's without interest with declaration (explicit).

    @-salutations
    --
    Michel Claveau



    Comment

    • Samuel Walters

      #3
      Re: explicit variable declaration

      |Thus Spake Luis Solís On the now historical date of Mon, 05 Jan 2004
      08:23:56 +0000|
      [color=blue]
      > Hi
      > It is possible to declare some variables as int, long... ? , or something
      > like visual basic option explicit.
      > In some situations could be usefull.
      > Thanks[/color]

      Well, in python, that is left to programmer's discretion.
      Once you get used to it, it's less of a hassle and danger zone than you'd
      expect.

      You can read GvR's (Our Benevolent Dictator For Life) thoughts
      on contracts in python here:


      As a fallback, you can always use the isinstance() and type() functions to
      check what type of variable you have on hand.

      Generally, isinstance is what you want to use because it returns true for
      subclasses. If the subclass doesn't do everything you'd expect of it's
      superclass, then either your or the subclass writer have broken their
      contract.

      If you absotively posolutely must know that a variable is a certain class,
      not a subclass and nothing but the bona fide variable type you expected,
      then use type(). This, however, is seldom the case because using type()
      could prevent future programmers from extending your code.

      If you want even more assurances, look into pylint and pychecker:



      HTH

      Sam Walters.

      p.s. The python-tutor list is probably a better place to ask questions
      like this. You're most likely to get helpful answers to questions about
      the basics than you are here.

      --
      Never forget the halloween documents.

      """ Where will Microsoft try to drag you today?
      Do you really want to go there?"""

      Comment

      • Peter Hansen

        #4
        Re: explicit variable declaration

        Michel Claveau/Hamster wrote:[color=blue]
        >
        > Hi !
        >
        > "Variable" is a bad word - beurk ! - In Python there are ref-on-objects ;
        > it's without interest with declaration (explicit).[/color]

        I'm not sure how helpful that is to a newbie, and in any case I've
        been using Python for a reasonably long time now and I definitely
        refer to those babies as variables.... with no ill effects to date.

        -Peter

        Comment

        • Dan Bishop

          #5
          Re: explicit variable declaration

          Samuel Walters <swalters_usene t@yahoo.com> wrote in message news:<pan.2004. 01.05.21.05.32. 783610@yahoo.co m>...[color=blue]
          > |Thus Spake Luis Solís On the now historical date of Mon, 05 Jan 2004
          > 08:23:56 +0000|
          >[color=green]
          > > Hi
          > > It is possible to declare some variables as int, long... ? , or something
          > > like visual basic option explicit.
          > > In some situations could be usefull.
          > > Thanks[/color]
          >
          > Well, in python, that is left to programmer's discretion.
          > Once you get used to it, it's less of a hassle and danger zone than you'd
          > expect.[/color]

          The main reason for this is that, in Python, using an unassigned
          variable gives you a friendly NameError. In BASIC, it implicitly
          creates a new variable (with a value of zero) whether you wanted it to
          or not.

          ....[color=blue]
          > As a fallback, you can always use the isinstance() and type() functions to
          > check what type of variable you have on hand.[/color]

          Or more accurately, what type of *object* you have on hand.

          Comment

          Working...