Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User Subclasses
#1
Hi,

My current project implementation provides a base class and a child class which provides variables for the base class to use in its methods. For example:

class BaseClass(object):
    
    def print_name(self):
        print(self.name)

class Person1(BaseClass):

    name = "Bob"

bob_inst = Person1()
bob_inst.print_name() 
My project generates custom child classes with unique names that are based on a dataset. The child classes are always inheriting the same base class. The child classes kinda act as containers for unique data, and the base class basically provides different ways to access that child class info.

I'd like to expand my project to provide a subclassable BaseClass to provide a different implementation for specific methods in the BaseClass. For example:

class BaseClass(object):
    
    def print_name(self):
        print(self.name)

class CustomClass(BaseClass):
    
    def print_name(self):
        print("Hello my name is %s" % self.name)

class Person1(CustomClass):

    name = "Bob"

bob_inst = Person1()
bob_inst.print_name() 
The above works fine, but my problem is I'd like the CustomClass in the example to be user-defined. So I won't be able to predict what class name they use. If I can't predict the classname, then my Child class generator cannot provide the correct Base Class to inherit.

My idea is to use the __subclasses__ variable to check if base class has any subclasses, and pass that conditionally to the Child class:

class BaseClass(object):
    
    def print_name(self):
        print(self.name)

class CustomClass(BaseClass):
        
    def print_name(self):
        print("Hello my name is %s" % self.name)                                                                                                                                              

if (len(BaseClass.__subclasses__()) > 0): 
    inherited_name = BaseClass.__subclasses__()[0]
else:
    inherited_name = BaseClass

class Person1(inherited_name):

    name = "Bob"

bob_inst = Person1()
bob_inst.print_name()
Seems a bit hacky to me, so was wondering if there is some better implementation of what I'm trying to do. Another problem I haven't really thought about is that the CustomClass that the user defines will probably live in a different file, so figuring out the importing might be tricky as well...

Thanks!
Reply


Messages In This Thread
User Subclasses - by holyghost - Mar-16-2021, 08:10 PM
RE: User Subclasses - by buran - Mar-16-2021, 08:47 PM
RE: User Subclasses - by holyghost - Mar-16-2021, 11:40 PM
RE: User Subclasses - by deanhystad - Mar-17-2021, 03:36 AM
RE: User Subclasses - by buran - Mar-17-2021, 12:03 PM
RE: User Subclasses - by deanhystad - Mar-17-2021, 12:18 PM
RE: User Subclasses - by buran - Mar-17-2021, 12:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  "unexpected keyword arg" when initializing my subclasses Phaze90 3 3,233 Nov-25-2022, 07:39 PM
Last Post: Gribouillis
  which design / pattern when building classes and subclasses Phaze90 2 1,144 Nov-19-2022, 08:42 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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