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

rh0dium sklass at pointcircle.com
Thu Aug 25 20:48:48 CEST 2005


Slight correction..

rh0dium wrote:
> 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):
    class uptime(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):
    class uptime(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?




More information about the Python-list mailing list