1

I'm trying to do something I consider pretty ordinary in Object Oriented programming but can't find any documentation on it for Python 2.7 I want to create a subclass of a superclass where, when I'm done superclass is bound to the superclass superclass.subclass is bound to the subclass and subclass is bound to nothing. Here's the best solution I can come up with:

class superclass:
 pass
class subclass(superclass):
 pass
superclass.subclass = subclass
del subclass

Sometimes I want subclass to be in its own file, other times not. Is there a more elgant solution where I don't have to manually perform the last two lines? Although

class superclass:
 class subclass:
 pass

almost does the right thing, subclass doesn't actually inherit from superclass. And if I try to do:

class superclass:
 class subclass(superclass):
 pass

I get an error message that superclass is unbound.

agf
178k45 gold badges300 silver badges241 bronze badges
asked Apr 19, 2012 at 5:19
2
  • 6
    Why do you want to do this? The best way is to have both in a module and only import superclass if you don't want subclass in your namespace. Commented Apr 19, 2012 at 5:29
  • 4
    This is nothing to do with OOP, perhaps you are porting ideas from another language that forces these kind of perversions Commented Apr 19, 2012 at 5:34

2 Answers 2

1

I agree with everyone else that this is a silly thing to do and I don't think you should do it, but in the interest of knowledge:

class Superclass(object):
 pass
Superclass.Subclass = type('Subclass', (Superclass,), {
 'foo': lambda self: self,
})

where the dictionary argument contains any methods. I don't think there's a nice way to do this with the class syntax, but that's really just syntactic sugar for calling type anyway.

You don't have to define the methods as lambdas; they can be normal functions with def as long as they have the self argument. You'll probably want to hide their definitions if you do that, though....

answered Apr 19, 2012 at 5:37
Sign up to request clarification or add additional context in comments.

1 Comment

That does answer the question, but surely that's a lot less elegant if the whole point is to save lines...
1

Here's a simple class decorator to do the referencing:

def refer(cls):
 cls.__bases__[0].subclass = cls
 return cls

Here's how you use it:

>>> class Superclass:
 pass
>>> @refer
 class Subclass(SuperClass):
 pass

You will still need to delete Subclass if you don't want the name in the module namespace.

answered Apr 19, 2012 at 6:11

1 Comment

You'd want setattr(cls.__bases__[0], cls.__name__, cls) to assign it to the name of the subclass; this would assign it as the literal name subclass, and so only works for one subclass.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.