explicit variable declaration
swalters_usenet at yahoo.com
Mon Jan 5 22:06:43 CET 2004
|Thus Spake Luis Solís On the now historical date of Mon, 05 Jan 2004
> It is possible to declare some variables as int, long... ? , or something
> like visual basic option explicit.
> In some situations could be usefull.
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
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
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:
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?"""
More information about the Python-list