Inheritance crossover packages

J

Johannes Bauer

Hello group,

I'm having a seemingly simple problem. I want to generate a hierarchy of
modules, like this one:

GenerationScripts/
GenerationScripts/dhcp
GenerationScripts/bind9

And the files:

GenerationScripts/dhcp/__init__.py
GenerationScripts/bind9/generator.py
GenerationScripts/bind9/__init__.py
GenerationScripts/mastergen.py
GenerationScripts/__init__.py

All packages (bind9, dhcp) should inherit from the master generator
"mastergen".

I'm at the very beginning:

$ cat GenerationScripts/__init__.py
import bind9
#import dhcpd

$ cat GenerationScripts/bind9/__init__.py
import GenerationScripts.bind9.generator

$ cat GenerationScripts/bind9/generator.py
from GenerationScripts import mastergen

class generator(mastergen):
def __init__(self):
print "init bind9 generator"

Now what happens when I import GenerationScripts and try to create a
instance of GenerationScripts.bind9.generator is the following:

Traceback (most recent call last):
File "./Generate.py", line 3, in <module>
import GenerationScripts
File "/home/joe/x/GenerationScripts/__init__.py", line 3, in <module>
import bind9
File "/home/joe/x/GenerationScripts/bind9/__init__.py", line 4, in
<module>
import GenerationScripts.bind9.generator
File "/home/joe/x/GenerationScripts/bind9/generator.py", line 7, in
<module>
class generator(mastergen):
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)

I really don't get it - and I've searched and found the same problem,
read the descirption - but still don't get it. What's happening here?

Regards,
Johannes
 
B

Bruno Desthuilliers

Johannes Bauer a écrit :
Hello group,

I'm having a seemingly simple problem. I want to generate a hierarchy of
modules, like this one:

GenerationScripts/
GenerationScripts/dhcp
GenerationScripts/bind9

And the files:

GenerationScripts/dhcp/__init__.py
GenerationScripts/bind9/generator.py
GenerationScripts/bind9/__init__.py
GenerationScripts/mastergen.py
GenerationScripts/__init__.py

All packages (bind9, dhcp) should inherit from the master generator
"mastergen".

Sorry but this makes no sense. inheritance is a class feature - packages
and modules don't 'inherit'.
I'm at the very beginning:

$ cat GenerationScripts/__init__.py
import bind9
#import dhcpd

$ cat GenerationScripts/bind9/__init__.py
import GenerationScripts.bind9.generator

$ cat GenerationScripts/bind9/generator.py
from GenerationScripts import mastergen

class generator(mastergen):

def __init__(self):
print "init bind9 generator"

here, mastergen is a module object, not a class object. This just can't
work. What are you trying to do exactly ???
Now what happens when I import GenerationScripts and try to create a
instance of GenerationScripts.bind9.generator is the following:

Traceback (most recent call last):
File "./Generate.py", line 3, in <module>
import GenerationScripts
File "/home/joe/x/GenerationScripts/__init__.py", line 3, in <module>
import bind9
File "/home/joe/x/GenerationScripts/bind9/__init__.py", line 4, in
<module>
import GenerationScripts.bind9.generator
File "/home/joe/x/GenerationScripts/bind9/generator.py", line 7, in
<module>
class generator(mastergen):
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)
>
I really don't get it - and I've searched and found the same problem,
read the descirption - but still don't get it. What's happening here?

What's happening is that you try to use a module instance (really: an
instance of class 'module') as a base class - and, as a result, the
'module' class as a metaclass. But 1/ the 'module' class initializer's
signature is not compatible with a metaclass signature, and 2/ anyway,
the 'module' class constructor doesn't return a class object anyway.

I don't know what you trying to do, but I suggest you (re)read the
FineManual(tm)'s relevant sections, that is, sections about modules and
packages, and sections about Python's 'new-style' object model.
 
C

Carl Banks

Hello group,

I'm having a seemingly simple problem. I want to generate a hierarchy of
modules, like this one:

GenerationScripts/
GenerationScripts/dhcp
GenerationScripts/bind9

And the files:

GenerationScripts/dhcp/__init__.py
GenerationScripts/bind9/generator.py
GenerationScripts/bind9/__init__.py
GenerationScripts/mastergen.py
GenerationScripts/__init__.py

All packages (bind9, dhcp) should inherit from the master generator
"mastergen".

I'm at the very beginning:

$ cat GenerationScripts/__init__.py
import bind9
#import dhcpd

$ cat GenerationScripts/bind9/__init__.py
import GenerationScripts.bind9.generator

$ cat GenerationScripts/bind9/generator.py
from GenerationScripts import mastergen

class generator(mastergen):
        def __init__(self):
                print "init bind9 generator"

mastergen is a module, so you can't subclass it. Try changing the
import line to:

from GenerationScripts.mastergen import mastergen


BTW, the error message is misleading, and it seems like it'd be a
common error. Maybe it deserves a special case error message. (I.e.
if the call to metaclass.__init__ raises TypeError, check to see if
it's a module and raise a better error message.)


Carl Banks
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top