-1

I have two classes. In one class, I have lots of methods that run based one on variable.

And in a second class, I want to define a second variable, where the methods of class one run, when this variable is true.

However, I'm not sure how to write this.

I want to know how do define the new variable (in the second class) and ensure this variable in the second class replaces the variable in the first class (when this variable is true).

For example:

class One():
   def MethodOne( self ):

        if self.this.VariableOne:
          do something
          do something else
          do another thing
          return something

class Two(One):
   def MethodOne( self ):

        if self.this.VariableTwo:
          run all the other code in MethodOne(), (if the variable is VariableTwo, but replacing VariableOne.

I'm not sure on the best approach here.

But I still want the MethodOne method to run (from a different class), but using a different variable instead.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Reena Verma
  • 1,617
  • 2
  • 20
  • 47
  • Could you clarify the behaviour? In general, if you want to call the *inherited* version of the method, you use `super`. – jonrsharpe Jan 14 '19 at 10:53
  • The behavior you mention here is unclear. You could initialise: `class classone(object): def __init__(self, input): self.VariableOne = input` – crowgers Jan 14 '19 at 11:14

2 Answers2

0

If I understand your question correctly, the simplest way to achieve this I think would be to create a class variable already on One, just set it to false. Something like:

class One():
   force = False

   def MethodOne( self ):
        if self.this.VariableOne or self.force:
          do something
          do something else
          do another thing
          return something

class Two(One):
    force = True
mfrackowiak
  • 1,294
  • 8
  • 11
  • This kinda is what I'd like to do, but class One(), contains tens and tens of methods in reality. So I don't want to write an if/else statement for each. I just want Class Two(), to automatically run it's variable, for the methods in ClassOne / Method One(), if that makes sense? – Reena Verma Jan 14 '19 at 11:04
0

I think the best way would be to use a decorator for all functions, implemented in a metaclass (from Attaching a decorator to all functions within a class):

class TheMeta(type):
    def __new__(cls, name, bases, namespace, **kwds):
        # my_decorator = cls.my_decorator (if the decorator is a classmethod)
        namespace = {k: v if k.startswith('__') else Variablecheck(v) for k, v in namespace.items()}
        return type.__new__(cls, name, bases, namespace)

The decorator:

def Variablecheck(func):
    @functools.wraps(func)
    def wrapper_Variablecheck(*args, **kwargs):
        # if inspect.isclass(type(args[0])):
        if args[0].this.VariableTwo:
            return func(*args, **kwargs)

then, you give it a try with:

class One(metaclass=TheMeta):
    # rest of the definition

Else you could also apply the metaclass to an inheriting class, if you want to keep the original class as is.

bklebel
  • 66
  • 5