explicit variable declaration

Discussion in 'Python' started by Luis Solís, Jan 5, 2004.

  1. Luis Solís

    Luis Solís Guest

    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
    Luis Solís, Jan 5, 2004
    #1
    1. Advertising

  2. Hi !

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

    @-salutations
    --
    Michel Claveau
    Michel Claveau/Hamster, Jan 5, 2004
    #2
    1. Advertising

  3. |Thus Spake Luis Solís On the now historical date of Mon, 05 Jan 2004
    08:23:56 +0000|

    > 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


    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:
    http://www.artima.com/intv/pycontract.html

    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:
    http://www.logilab.org/projects/pylint
    http://pychecker.sourceforge.net/

    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.
    http://www.opensource.org/halloween/
    """ Where will Microsoft try to drag you today?
    Do you really want to go there?"""
    Samuel Walters, Jan 5, 2004
    #3
  4. Luis Solís

    Peter Hansen Guest

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


    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
    Peter Hansen, Jan 5, 2004
    #4
  5. Luis Solís

    Dan Bishop Guest

    Samuel Walters <> wrote in message news:<>...
    > |Thus Spake Luis Solís On the now historical date of Mon, 05 Jan 2004
    > 08:23:56 +0000|
    >
    > > 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

    >
    > 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.


    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.

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


    Or more accurately, what type of *object* you have on hand.
    Dan Bishop, Jan 8, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.T. Conklin
    Replies:
    1
    Views:
    428
    David Hilsee
    Aug 11, 2004
  2. Andy
    Replies:
    5
    Views:
    494
    Shezan Baig
    Jan 30, 2005
  3. Bolin
    Replies:
    4
    Views:
    394
  4. Replies:
    1
    Views:
    559
    Salt_Peter
    Dec 25, 2006
  5. Filip Gruszczyński

    Explicit variable declaration

    Filip Gruszczyński, Apr 23, 2008, in forum: Python
    Replies:
    8
    Views:
    599
    Filip Gruszczyński
    Apr 25, 2008
Loading...

Share This Page