G
G
gadzhi152016-04-21 13:54:30
Python
gadzhi15, 2016-04-21 13:54:30

Calling all class methods in one line. Maybe?

Python 2.7. There is a class with 5 methods and a constructor __init___ . In a certain situation, you need to call all the methods of the class. Is it possible to do this in one line in Python?
Java has a Builder like this one.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2016-04-21
@gadzhi15

I don’t quite understand why, but in order to call functions in one line, it is enough to return self in the function at the end.

class S():
    kk = 0
    def m(self):
        kk+=1
        return self
    
    def p(self):
        print(self.kk)
        return self

s = S().m().m().m().p().m().m().p()

will output "3" and "5"

S
Sergey Gornostaev, 2016-04-21
@sergey-gornostaev

import inspect
for _, m in inspect.getmembers(obj, predicate=inspect.ismethod): m()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question