0

I'm building some code from a stylesheet, but I also want to have the ability to add a custom class in python. However, sometimes, the custom class won't exist. For example, I have two classes: foo_base, and foo_custom. I want to define a class foo that can extend both classes, or just foo_base if foo_custom doesn't exist

try:
    def foo(foo_base, foo_custom):
except:
    def foo(foo_base):
...
        def __init__(self):
            ...

Hopefully that makes sense. Essentially, I want to extend the class if it exists.

Thanks!

YatShan
  • 425
  • 2
  • 8
  • 22
Conrad
  • 239
  • 2
  • 8
  • How does your module determine/add `foo_custom` into place? What I mean is do you have an import statement of some kind? – metatoaster Jun 19 '19 at 01:11
  • You can extend a class, only if exists in any language. This is referred to as Inheritence in OOP. Please refer following link, to get more insight.https://www.w3schools.com/python/python_inheritance.asp – YatShan Jun 19 '19 at 01:12
  • Agreed with @metatoaster that it needs import statement at the top of your code snippet. – YatShan Jun 19 '19 at 01:14
  • Are the `def foo(...)` statements in your code supposed to be `class foo(...)` statements? – Blckknght Jun 19 '19 at 02:03

2 Answers2

0

Rather than using an exception of some kind for handling just two case, the parent class argument in the type constructor (i.e. class statement) allow the argument expansion of a list in Python 3. Such that what you could do for example is:

parent_classes = [MainParent]

# it is also possible to make use of `importlib` to go through a
# list of possible imports rather than doing this one-by-one in
# this verbose manner
try:
    from might_be_missing import SomeOtherClass
except ImportError:
    pass  # perhaps log a warning of some kind
else:
    parent_classes.append(SomeOtherClass)

# To create the class

class YourClass(*parent_classes):
    """
    The main class definition
    """

However, this particular pattern is typically not a useful pattern for the ultimate end-users of this class necessarily, because this doesn't provide a stable behavior that users can easily infer given that this is stuck behind a system they may not necessarily have control over. A more useful technique over class inheritance could be class composition for the problem you might be actually solving.

Please also see: Python: Inheritance versus Composition

metatoaster
  • 17,419
  • 5
  • 55
  • 66
0

You could use a registry to register your classes, and then check the registry to see if it exists.

class Registry(): #Creating Registry class

   def __init__(self):
      self.registry = []

   def register(self,class_):
      self.registry.append(class_) 
      return class_

   def check(self, class_):
       if class_ in self.registry:
           return True

registry = Registry()  #Creating Registry object

@registry.register  #Adding to the Registry
class Some():
     pass
Some()  #Creating class instance
print(registry.check(Some))  #Checking if it exists
#Output
#True

Then its just a matter of calling

if registry.check(your_class):
   #Do something```
Robert Kearns
  • 1,631
  • 1
  • 8
  • 15