Tuesday 4 December 2007

python: import and Class and dir()

Python:
No matter what the current directory is, Python must import the things it need.
---------------------------------------------------------
Java:
If the class or jar is in the CLASSPATH, import will cause errors like: "import abc; something is expected"

############################
Import for Python:

  • What is imported is just the module name. If in the module a class is defined, to get a instance of the class, you must
    >>>import abc # here abc is the module file name
    >>>instance=abc.Cons() # here Cons is the class constructor; for jython, abc=Cons, it will looks like >>>instance=abc.abc()
  • >>>dir(instance)
    [] # this is just what you got when you want to check what methods is available for the object. If you want to achieve it, you should run:
    >>>dir(abc.Cons) # in Jython it should be: >>>dir(abc.abc)

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter