Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mask superclass __init__
#1
Is there anyway to hide the superclass initialization from the user?

For example:
class BaseClass:
    def __init__(self):
        self.param = 10

class OtherClass(BaseClass):
    # I don't want the OtherClass to explicit call the baseclass initialization like this
    def __init__(self):
        super().__init__()
Reply
#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
#3
Quote:Why do you want to hide it? It is very difficult to hide anything in python code.

I think it becomes more simple and clean to the framework user to not directly call superclass init .

Quote:Would a metaclass suit your needs?

Great! I saw something familiar in Django code.
The django.db.Model does not need to be initialized since it uses a metaclass to initialize its properties.
Reply
#4
(Jan-10-2018, 09:36 PM)fig0 Wrote: I think it becomes more simple and clean to the framework user to not directly call superclass init .
The drawback of this is that you cannot fine tune the constructor's parameters. Simplicity has a cost...
Reply
#5
Can't I simple use the class __new__ magic method instead of creating a metaclass?
Reply
#6
(Jan-11-2018, 05:45 PM)fig0 Wrote: Can't I simple use the class __new__ magic method instead of creating a metaclass?
I don't think so. There is nothing magic in the __new__() method. Its role is to create a raw object prior to intialization. I wouldn't try to use __new__() to avoid calling the ancestor classes' constructor. It is not good design, and it will be difficult to handle several levels of inheritance.

Python provides metaclasses as the normal way to handle such specific type requests. What do you think is wrong with using a metaclass?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I mask Hex value wiht 0xFF? korenron 2 4,351 Nov-23-2021, 09:13 AM
Last Post: Gribouillis
  Using boolean mask in Numpy for 3D IlikePi 0 1,471 Nov-14-2020, 10:08 PM
Last Post: IlikePi
  superclass and super() grkiran2011 1 1,693 Jun-20-2020, 04:37 AM
Last Post: deanhystad
  Is it mandatory to call superclass init inside the class init? psolar 3 5,919 Feb-14-2020, 09:16 PM
Last Post: wavic
  Saving a mask as a png file in opencv DanTheMan 1 5,604 Jan-31-2020, 09:20 AM
Last Post: ThiefOfTime
  Subnet Mask Ranges ab52 0 1,786 Mar-11-2019, 10:39 AM
Last Post: ab52
  Create a List for Boolean Mask. leoahum 4 3,799 Oct-09-2018, 02:38 PM
Last Post: leoahum
  python script to get wildcard mask output in the following format techrichit 0 3,779 Aug-10-2018, 11:01 PM
Last Post: techrichit
  Why is there an __init__ statement within the __init__ definition iFunKtion 7 5,962 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