Some Newbie Questions

Matteo Dell'Amico della at toglimi.linux.it
Wed May 26 08:23:10 EDT 2004


Leopold Schwinger wrote:
> (1) Is there any operator in Python in order to call a Member-Function 
> from a class without creating an instance of the class? (In C++ and PHP 
> there ist this operator "::" <class>::<memberfunction>)

Just call it. :-)

When you are using methods from instances, the first argument (the one 
that is usually called "self") is automagically bound to the instance. 
When you access it from the class, this won't happen (and the method, in 
this case, is called "unbound").

For instance:
 >>> class A(object):
...     def foo(self):
...             print self
...
 >>> a = A()
 >>> a.foo()
<__main__.A object at 0x401f7d6c>
 >>> A.foo(a)
<__main__.A object at 0x401f7d6c>
 >>> A.foo
<unbound method A.foo>
 >>> a.foo
<bound method A.foo of <__main__.A object at 0x401f7d6c>>

You have an interactive prompt, try playing with it! :-)

> (2) Is there any way to dynamically import modules, where the name of 
> the module is not known during implementation but can be defined durch
> scipt execution? Think of lots of different config-Files, which differ 
> by Path & Name but the variables are all named equal, and i want to 
> import a special config-File during the execution of the script.

You can use the __import__ built-in. For your problem, look at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/283531 .

> (3) Is there any way to obtain a kind of "trace"-Info (the actual 
> filename and codeline) in order to provide some debug-Info in the case 
> of errors (In C++ and PHP there are these "__line__" and "__file__" 
> functions, For Python I have only found <Class>.__dict__ and dir(<Class>))

traceback module. Keep the Library Reference under your pillow! :-)

-- 
Ciao,
Matteo



More information about the Python-list mailing list