0

Let's say I have a class, classA, with the method functionA1 in it. I can then use this class as followed:

instance = classA()
instance.functionA1()

How would I chain methods or classes? For example:

# 1. Function A2 only available after Function A1, as it uses results from Function A1
A().F_A1().F_A2()

# 2. ClassB and it's methods only available behind Function A1
A().F_A1().B().F_B1()

# 3. ClassB and it's methods only available behind classA
A().B()

# Example:
cars().brand('Audi').color('red')
#-> return red Audi's
Cas
  • 179
  • 7
  • Are you sure you want that ? Because regarding the last example `cars().color('red').brand('Audi')` could be ok regardind the Builder pattern rules. And in that case code is easy to make – azro Nov 20 '21 at 09:19

1 Answers1

0

I don't think that is possible in python.

Instead, perhaps set a variable like function_1_finished = True and check if it was set before running function 2

class a:
    class b:
        def run(self):
            self.stuff = None
            print("b is running")
    def run(self):
        self.other_stuff = None
        print("a is running")

    def makeB(self):
        self.B= self.b()

A = a()
A.run()
A.makeB()
A.B.run()
Delta qyto
  • 125
  • 8
  • Yes it is possible. For example in the documentation of python-plexapi, a tool written fully in python, the first example shows this: `movies = plex.library.section('Movies')`. So yes, it IS possible. And I'm asking how they're doing it. `plex` is the instance created and then they use the method `library`. That's normal. But I'm asking how they made it possible to have that `section()' class behind it. It's a class because they put the result in a variable so it's an instance. – Cas Nov 20 '21 at 11:10
  • Sorry, I think I misunderstood your question. If you wish to have a class belong to another class, you can simply nest them, my original answer has code attached for this. There are no inbuilt checks that a function executed before another has, so that should be implemented by the programmer – Delta qyto Nov 21 '21 at 12:20