override class variable in python?

后端 未结 3 1024
灰色年华
灰色年华 2021-01-04 05:10

Below, base_id and _id is a class variable and shared among all child classes.
Is there a way to separate them into each class?



        
3条回答
  •  再見小時候
    2021-01-04 05:22

    If you don't want to violate the DRY principle like falsetru suggests, you'll need to use metaclasses. I was thinking of writing something up, but there's already a good long description of metaclasses on SO, so check it out.

    Metaclasses, in short, let you control subclass creation.

    Basically, what you need to do is, upon the creation of a subclass of Parent, add the _id member to the newly-created subclass.

提交回复
热议问题