Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mask superclass __init__
#2
Why do you want to hide it? It is very difficult to hide anything in python code.

Would a metaclass suit your needs?
class AutoSuperInitialized(type):
    def __new__(meta, name, superclasses, memberdict):
        i = '__init__'
        if i in memberdict:
            f = memberdict[i]
            def __init__(self, *args, **kwargs):
                super(cls, self).__init__(*args, **kwargs)
                f(self, *args, **kwargs)
            memberdict[i] = __init__
        cls = type.__new__(meta, name, superclasses, memberdict)
        return cls
    
    
class BaseClass(metaclass=AutoSuperInitialized):
    def __init__(self):
        print('BaseClass __init__ called!')
        
class OtherClass(BaseClass):
    def __init__(self):
        print('OtherClass __init__ called!')
        
if __name__ == '__main__':
    obj = OtherClass()
Exclamation it works even with sub-sub-classes!
Reply


Messages In This Thread
Mask superclass __init__ - by fig0 - Jan-10-2018, 04:30 PM
RE: Mask superclass __init__ - by Gribouillis - Jan-10-2018, 05:19 PM
RE: Mask superclass __init__ - by fig0 - Jan-10-2018, 09:36 PM
RE: Mask superclass __init__ - by Gribouillis - Jan-10-2018, 09:46 PM
RE: Mask superclass __init__ - by fig0 - Jan-11-2018, 05:45 PM
RE: Mask superclass __init__ - by Gribouillis - Jan-11-2018, 06:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I mask Hex value wiht 0xFF? korenron 2 4,720 Nov-23-2021, 09:13 AM
Last Post: Gribouillis
  Using boolean mask in Numpy for 3D IlikePi 0 1,588 Nov-14-2020, 10:08 PM
Last Post: IlikePi
  superclass and super() grkiran2011 1 1,814 Jun-20-2020, 04:37 AM
Last Post: deanhystad
  Is it mandatory to call superclass init inside the class init? psolar 3 6,324 Feb-14-2020, 09:16 PM
Last Post: wavic
  Saving a mask as a png file in opencv DanTheMan 1 5,820 Jan-31-2020, 09:20 AM
Last Post: ThiefOfTime
  Subnet Mask Ranges ab52 0 1,875 Mar-11-2019, 10:39 AM
Last Post: ab52
  Create a List for Boolean Mask. leoahum 4 4,030 Oct-09-2018, 02:38 PM
Last Post: leoahum
  python script to get wildcard mask output in the following format techrichit 0 3,903 Aug-10-2018, 11:01 PM
Last Post: techrichit
  Why is there an __init__ statement within the __init__ definition iFunKtion 7 6,193 Feb-06-2017, 07:31 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020