Re: Is it dangeous when using custom metaclass?
jf...@ms4.hinet.net writes: > ... > Hard to find the document of type.__init__. I can only guess it does nothing, > at least no thing serious, to avoid trouble the metaclass's __init__ may > cause in a class hierarchy:-) You always have the possibility to look at the source. All classes have common attributes "__bases__", "__module__", "__name__", "__dict__", They must come from somewhere. "type.__init__" could be this place. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is it dangeous when using custom metaclass?
dieter at 2018/10/17 UTC+8 PM 1:15:01 wrote: > jf...@ms4.hinet.net writes: > > Gregory Ewing at 2018/10/16 UTC+8 PM 2:01:01 wrote > >> jf...@ms4.hinet.net wrote: > >> > class Structure(metaclass=StructureMeta): ... > >> > > >> > class PolyHeader(Structure): ... > >> > > >> > As my understanding, the metaclass's __init__ was called when a class was > >> > created. In the above example, both the Structure and PolyHeader called > >> > it. > >> > My question is: because the PolyHeader inherited Structure, is it > >> > reasonable > >> > for PolyHeader to call this __init__ again? Will it cause any possible > >> > trouble? > >> > >> It's reasonable for both to call it, because they're distinct > >> instances of StructureMeta, each of which need to be initialised. > > > > The PolyHeader is already initialized by inheritance. Is there any way to > > bypass this __init__? > > If there were any, you should find it described in metaclass related > documentation. > > Likely, you can ensure that followup calls of "__init__" effectively > behave as "no-op"s: let the first call place a marker in the initialized > object and check in later calls whether it is already there. Hard to find the document of type.__init__. I can only guess it does nothing, at least no thing serious, to avoid trouble the metaclass's __init__ may cause in a class hierarchy:-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Is it dangeous when using custom metaclass?
jf...@ms4.hinet.net writes: > Gregory Ewing at 2018/10/16 UTC+8 PM 2:01:01 wrote >> jf...@ms4.hinet.net wrote: >> > class Structure(metaclass=StructureMeta): ... >> > >> > class PolyHeader(Structure): ... >> > >> > As my understanding, the metaclass's __init__ was called when a class was >> > created. In the above example, both the Structure and PolyHeader called it. >> > My question is: because the PolyHeader inherited Structure, is it >> > reasonable >> > for PolyHeader to call this __init__ again? Will it cause any possible >> > trouble? >> >> It's reasonable for both to call it, because they're distinct >> instances of StructureMeta, each of which need to be initialised. > > The PolyHeader is already initialized by inheritance. Is there any way to > bypass this __init__? If there were any, you should find it described in metaclass related documentation. Likely, you can ensure that followup calls of "__init__" effectively behave as "no-op"s: let the first call place a marker in the initialized object and check in later calls whether it is already there. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is it dangeous when using custom metaclass?
Gregory Ewing at 2018/10/16 UTC+8 PM 2:01:01 wrote > jf...@ms4.hinet.net wrote: > > class Structure(metaclass=StructureMeta): ... > > > > class PolyHeader(Structure): ... > > > > As my understanding, the metaclass's __init__ was called when a class was > > created. In the above example, both the Structure and PolyHeader called it. > > My question is: because the PolyHeader inherited Structure, is it reasonable > > for PolyHeader to call this __init__ again? Will it cause any possible > > trouble? > > It's reasonable for both to call it, because they're distinct > instances of StructureMeta, each of which need to be initialised. The PolyHeader is already initialized by inheritance. Is there any way to bypass this __init__? > Whether it will cause a problem depends on what StructureMeta's > __init__ is supposed to do. Presumably you want a given structure > class to start allocating its offsets where its base class left > off, in which case you may need to do something like this: > > class StructureMeta(type): > def __init__(self, clsname, bases, clsdict): > if bases: > offset = bases[0].offset # assuming there isn't more than one base > else: > offset = 0 > ... > > (BTW, why do you use setattr() to set the offset attribute > instead of just doing self.offset = offset?) No particular reason, just follows the same code pattern ahead of it where the attribute names are from a list:-) --Jach -- https://mail.python.org/mailman/listinfo/python-list
Re: Is it dangeous when using custom metaclass?
jf...@ms4.hinet.net wrote: class Structure(metaclass=StructureMeta): ... class PolyHeader(Structure): ... As my understanding, the metaclass's __init__ was called when a class was created. In the above example, both the Structure and PolyHeader called it. My question is: because the PolyHeader inherited Structure, is it reasonable for PolyHeader to call this __init__ again? Will it cause any possible trouble? It's reasonable for both to call it, because they're distinct instances of StructureMeta, each of which need to be initialised. Whether it will cause a problem depends on what StructureMeta's __init__ is supposed to do. Presumably you want a given structure class to start allocating its offsets where its base class left off, in which case you may need to do something like this: class StructureMeta(type): def __init__(self, clsname, bases, clsdict): if bases: offset = bases[0].offset # assuming there isn't more than one base else: offset = 0 ... (BTW, why do you use setattr() to set the offset attribute instead of just doing self.offset = offset?) -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Is it dangeous when using custom metaclass?
class StructureMeta(type): def __init__(self, clsname, bases, clsdict): offset = 0 ... ... setattr(self, 'struct_size', offset) class Structure(metaclass=StructureMeta): ... ... class PolyHeader(Structure): ... ... As my understanding, the metaclass's __init__ was called when a class was created. In the above example, both the Structure and PolyHeader called it. My question is: because the PolyHeader inherited Structure, is it reasonable for PolyHeader to call this __init__ again? Will it cause any possible trouble? --Jach -- https://mail.python.org/mailman/listinfo/python-list