In brief, I'm trying to get a 'virtual class' thing going so that I can use it for generic Toplevel widgets whose behavior is specified by the inheriting class.

I have this code (Pyth. 2.4):

class Generic:

    def __init__(self):
        self.field = "hi"  # placeholder

    def gen_method(self):
        self.method()    # I'm hoping that this can be defined by the child

class Specific1(Generic):

    def __init__(self):
        super(Specific1,self).__init__()

    def method():  # defining here...
        print "Specific1 method!"

class Specific2(Generic):

    def __init__(self):
        super(Specific2,self).__init__()

    def method():  # and here...
        print "Specific2 method!"

a = Specific1()
b = Specific2()
a.gen_method()
b.gen_method()

Surprisingly, the code never gets out of the gate. Instead, I get:

Traceback (most recent call last):
File "<pyshell#2>", line 1, in -toplevel-
super(Specific1)
TypeError: super() argument 1 must be type, not classobj

If I take this at face value, it appears to mean that super(int) is valid, but super(MyInheritedClass) is not ... which is contrary to what the help docs say.

Any wisdom?

Thanks in advance,
Jeff Cagle

Fixed. I had "new-style" and "old-style" classes backwards in my mind.

This works:

# test of "virtual classes"

class Generic(object): # fixed!

    def __init__(self):
        self.field = "hi"

    def gen_method(self):
        self.method()

class Specific1(Generic):

    def __init__(self):
        super(Specific1,self).__init__()

    def method(self):  # fixed!
        print "Specific1 method!"

class Specific2(Generic):

    def __init__(self):
        super(Specific2,self).__init__()

    def method(self):  # fixed!
        print "Specific2 method!"

a = Specific1()
b = Specific2()
a.gen_method()
b.gen_method()

with output

Specific1 method!
Specific2 method!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.