1

As we can create multiple variables in a single line, such as:

x, y, z = 1, 2, 3
print(x)

and the output would be 1,

I wonder if there is something similar to create multiple empty classes, so something like this, which I know is wrong, but just to let you have an idea of what I mean:

class X, Y, Z:
    pass

Thank you very much!

1
  • I don't think this is meaningful as class is an abstract thing. Three identical abstract things can be abstacted as one thing. Commented Jul 6, 2018 at 9:08

1 Answer 1

4

No, there is no special syntax to use a class definition this way. You could use the type constructor, something like:

>>> A, B, C = type('A', (object,), {}), type('B', (object,), {}), type('C', (object,), {})
>>> A, B, C
(<class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>)
>>>
>>> A()
<__main__.A object at 0x10354d780>
>>> B()
<__main__.B object at 0x1038b1ef0>
>>> C()
<__main__.C object at 0x10354d780>

But I think that's hardly elegant, and it's unlikely you'll be able to keep the single line to a sane length. Just stick to the full class definitions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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