metaclass

Metaclass vs inheritance for predefined class creation

送分小仙女□ 提交于 2020-12-13 09:27:18
问题 I'm writing some code for a program that will be able to run some software, read the inputs/outputs and do some data processing along the way. For example: (this is not the real case, but just to give an idea) class MusicFile(object): extension = [""] def setup(self): #set up the data def runsoftware(self): #play the song class MP3(MusicFile): extension = [".mp3"] def setup(self): #setupMP3file def runsoftware(self): #runMP3software I have about 4 general classes, and then file extensions

Metaclass vs inheritance for predefined class creation

北战南征 提交于 2020-12-13 09:27:15
问题 I'm writing some code for a program that will be able to run some software, read the inputs/outputs and do some data processing along the way. For example: (this is not the real case, but just to give an idea) class MusicFile(object): extension = [""] def setup(self): #set up the data def runsoftware(self): #play the song class MP3(MusicFile): extension = [".mp3"] def setup(self): #setupMP3file def runsoftware(self): #runMP3software I have about 4 general classes, and then file extensions

Metaclass vs inheritance for predefined class creation

大兔子大兔子 提交于 2020-12-13 09:26:01
问题 I'm writing some code for a program that will be able to run some software, read the inputs/outputs and do some data processing along the way. For example: (this is not the real case, but just to give an idea) class MusicFile(object): extension = [""] def setup(self): #set up the data def runsoftware(self): #play the song class MP3(MusicFile): extension = [".mp3"] def setup(self): #setupMP3file def runsoftware(self): #runMP3software I have about 4 general classes, and then file extensions

Is it safe to use autocall __init__ in this way?

别等时光非礼了梦想. 提交于 2020-12-13 03:25:25
问题 I wanted to force children to call parent constructor and found this answer which seems to do the job fine. However, I'm a bit unsure if what I'm doing is safe. I have this: # Copied from answer linked above class meta(type): def __init__(cls,name,bases,dct): def auto__call__init__(self, *a, **kw): for base in cls.__bases__: base.__init__(self, *a, **kw) cls.__init__child_(self, *a, **kw) cls.__init__child_ = cls.__init__ cls.__init__ = auto__call__init__ # My code import unittest class

Is it safe to use autocall __init__ in this way?

为君一笑 提交于 2020-12-13 03:24:49
问题 I wanted to force children to call parent constructor and found this answer which seems to do the job fine. However, I'm a bit unsure if what I'm doing is safe. I have this: # Copied from answer linked above class meta(type): def __init__(cls,name,bases,dct): def auto__call__init__(self, *a, **kw): for base in cls.__bases__: base.__init__(self, *a, **kw) cls.__init__child_(self, *a, **kw) cls.__init__child_ = cls.__init__ cls.__init__ = auto__call__init__ # My code import unittest class