Superclass files and order - oh my!! Questioning the experts!!

R

rh0dium

Hi all,

Still a newbie but making some headway. So I have a file structure
like this..

top/
--modules/
----metrics.py
--metrix/
----uptime.py

Now metrics.py is my superclass, and uptime.py inherits the superclass
metrics.py.

So for arguments sake

metrics.py
----------------
class metrics():
def __init__(self, foo=bar):
self.foo=foo
def run():
pass
----------------


uptime.py
----------------
class metrics(metrics):
def run():
print "hello"

if __name__ == '__main__':

if os.path.exists("../modules"):
print "importing modules"
sys.path.insert( 0, "../modules")
import metrics
else:
print "Unable to bring in main metric instance - path not correct"
sys.exit(1)

----------------

Now if simply try to run ./uptime.py it complains about not having the
metrics module? Huh but I brought it in?

File "./uptime.py", line 19, in ?
class uptime(metrics):
NameError: name 'metrics' is not defined

BUT...

If I do this then it it imports.. ( NOTE the order is different.. )

uptime.py
----------------
if os.path.exists("../modules"):
print "importing modules"
sys.path.insert( 0, "../modules")
import metrics
else:
print "Unable to bring in main metric instance - path not correct"
sys.exit(1)

class metrics(metrics):
def run():
print "hello"

if __name__ == '__main__':
pass

----------------

But I get the next erorr:
Path exists
importing modules
Traceback (most recent call last):
File "./uptime.py", line 34, in ?
class uptime(metrics):
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)

So the question is..

Why is it not looking at the "if __name__ == '__main__':" statement
and starting there? I thought it was supposed to do that? Why can't
I just put a class statement in a file and if you put nothing (like if
__name__....) the class is just "registered" so later if (like
__main__) I use it then it's available.

I want to not have to put that ugly code at the top but it seems as
though that's the only way? Someone must have figured this out and can
you explain why it workst this way?

I must be missing something cause that's how modules work. How is this
different than a module?
 
D

Dennis Lee Bieber

Why is it not looking at the "if __name__ == '__main__':" statement
and starting there? I thought it was supposed to do that? Why can't
I just put a class statement in a file and if you put nothing (like if
__name__....) the class is just "registered" so later if (like
__main__) I use it then it's available.
That if structure is essentially used to PREVENT code from being run
when the file is invoked as an imported module; it has nothing to do
with specifying code to be run ONLY.

class xya(b):
...

IS an EXECUTABLE statement in Python, not a declaration that is
deferred until later use (so are def sss():, etc.).

As an executable statement, the parent class (inside the paren's)
MUST exist when the class statement is processed. In your case, this
means it needs to have been imported before the class definition is
executed -- not when an instance of the class is created.

--
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top