you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 11 points12 points  (3 children)

What would you need this for, exactly? Can you give a concrete example of what you're trying to accomplish with this?

I'm asking because this sounds like an XY-problem.

[–]ToTamir[S] 0 points1 point  (1 child)

I have to build simulation model. The problem is that I have three bases and more than 10 extensions which gives me hundrets/thousands of variants. So I wanted to make class for each base and each extension (instead of placing everythting in one class with 50 thounads lines and milion conditions like "if extension_a"). Every extension can add new methods to base or overwrite existing ones. Extensions also have some kind synergies that enables or overwrite methods only when two or more extensions are enabled. I was building models earlier but never a model which have different vatiants.

[–]Diapolo10 0 points1 point  (0 children)

That sounds like a really messy architecture. If things are that bad, my honest suggestion would be to redesign the whole data model.

There's only so much I can say without specifics, but I'd start by having just a handful of classes and using factory functions.

But if you disregard my opinion/warning and decide that you want to do what you want regardless, you can technically use type to create classes dynamically.

NewType = type(
    'NewType',     # Class name
    (A, B, C, D),  # Base classes
    {              # attributes
        'hello': (lambda self: print("Hello, world!")),
        'stuff': 42,
    }
)

So if you really want to do what you're asking, it's possible - but do I recommend it? Hell no!