Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

jobs.webdeveloper.com

e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Intro to Python Introspection and Dynamic Programming

The Type Function

Python supports dynamic typing, meaning that any symbol in the namespace can at anytime point to any type of object. Obviously, this could cause problems, fortunately the built- in function type() can be used to prevent problems. Hence:

>>> var1 = 12
>>> typ = str(type(var1))
>>> if typ == "<type 'int'>":
...     var1 / 4
... else: print 'Not an Int'
3
>>> var1 = 'abc'
>>> typ = str(type(var1))
>>> if typ == "<type 'int'>":
...     var1/4
... else: print 'Not an Int'
Not an Int

Python even allows every member of a list or dictionary, even the keys, to be an object of any type. This can be very useful in organizing data and procedures in a program in a style of a database. A simple example:

#!/usr/bin/env python
class demo:
    def __init__(self,empname, rate, action):
        self.__empname = empname
        self.__rate    = rate
        self.__action  = action

    def getVals(self):
        return self.__dict__

def domain():
    demo1 = demo('john doe', 12, 'regpay')
    vals  = demo1.getVals()
    for k in vals.keys():
        print k, str(type(vals[k]))

if __name__ == '__main__':
    domain()

Running this program yields:
_demo__empname <type 'str'> _demo__action <type 'str'> _demo__rate <type 'int'>

Dir and Help

Python has many built-in modules. For instance one of the most useful is sys.

>>> import sys

And we can find out quickly what methods and properties it contains:

>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', 
'__stderr__', '__stdin__', '__stdout__', 
'_current_frames', '_getframe', 'api_version', 'argv', 
'builtin_module_names', 'byteorder', 
'call_tracing', 'callstats', 'copyright', 'displayhook', 'exc_clear', 
............
'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 
'subversion', 'version', 'version_info', 'warnoptions']

Notice it contains something called stderr, which we can discover to be a file:

>>> type(sys.stderr)

And if we need to know more, the following will give a fairly good description of the object type file:

>>> help(sys.stderr)

Python Introspection
Python Introspection
Dynamic Programming


Up to => Home / Authoring / Tutorials / Python




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers