Some Newbie Questions

L

Leopold Schwinger

Hello!

I am a really newbie to python, and I have some questions. I couldn't
find anything in the manual, so it would be nice if you could help me.

(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>)

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

(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>))

Thank you very much,
Leo
 
M

Matteo Dell'Amico

Leopold said:
(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:.... def foo(self):
.... print self
....<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! :)
 
L

Leopold Schwinger

Matteo said:
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:
... def foo(self):
... print self
...
<bound method A.foo of <__main__.A object at 0x401f7d6c>>

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



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



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

Thank you for your anwsers, i will play around a bit...
 
L

Larry Bates

On question #1: I see this question a lot on
this list, but I'm curious about why you don't
want to instantiate the class? I can't think
of a reason not to and am very curious if I'm
missing something here???

On question #2:

I normally write my programs so that they can be
called with an option on the processor call line
to override the "config-file". Something like:

python foo.py -i newconfigfile.ini

Then I use ConfigParser to read the options from
the .INI file. This provides unlimited flexibility
in having .INI files to drive the program.

I find that reading config parameters from an
external .INI file to be superior to putting
values in a .PY file and importing it. I know
others on this list may differ.

On question #3:

I use loggerclass to keep trace/debug information
in a logfile as the program executes. Actually I
normally have debug "levels" that give increasingly
more information as the number increases. I read
these from .INI file but allow user to override the
..INI file on the processor call line.

Example:

python foo.py -t -d 2

would turn trace on and debug at level 2

Then put lines in your code like:

if trace: log("Entering function a")

HTH,
Larry Bates
Syscon, Inc.
 
S

Stefan Meier

On question #1: I see this question a lot on
this list, but I'm curious about why you don't
want to instantiate the class? I can't think
of a reason not to and am very curious if I'm
missing something here???
Static classes for example. If you want to maintain object orientation and
have stuff to store statically, you might want to put it into a class and
maybe even do some data mangling in a method or so, but since the class is
static, no need to instantiate it.

- Stefan

--
Stefan Meier
Computational Biology & Informatics
Exelixis, Inc.
170 Harbor Way, P.O. Box 511
South San Francisco, CA 94083-511
fon. (650)837 7816
 
S

Slawomir Nowaczyk

On Wed, 26 May 2004 12:56:21 +0200

#> (2) Is there any way to dynamically import modules, where the name
#> of the module is not known during implementation but can be defined
#> during scipt execution?

Sure:

Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.<socket._socketobject object at 0x00952750>

--
Best wishes,
Slawomir Nowaczyk
( (e-mail address removed) )

Write your questions down on the back of $20 dollar bill and send them to me.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top