Answer the question
In order to leave comments, you need to log in
How to execute multiple methods at one time python?
How can you execute multiple class methods with one line of code?
For example:
class hi():
def hello(self):
return str("Hello")
def world(self, hello_str):
tmp = hello_str + "World!"
return tmp
caller = hi()
print(caller.hello().world())
Answer the question
In order to leave comments, you need to log in
a couple of ways
If you want the last method to return a result, you can do this:
class Hello(object):
def __init__(self):
self.msg = 'Hello'
def hello(self, world):
self.msg = self.msg + ', ' + world
return self
def print(self):
return self.msg
caller = Hello()
msg = caller.hello('niriter').print()
print(msg) # Hello, niriter
class Hello(object):
def __init__(self, msg='Friend'):
self.msg = msg
def print(self):
init_msg = self.hello()
return init_msg + ', ' + self.msg
def hello(self): # этот метод вызываем изнутри другого метода
return str("Hello")
caller1 = Hello()
print(caller1.print()) # Hello, Friend
caller2 = Hello('Maks')
print(caller2.print()) # Hello, Maks
This is called chaining. This is done using the builder pattern. The bottom line is that each such function returns a copy of the object that called it (or a pointer to it).
class Person:
def setName(self, name):
self.name = name
return self ## this is what makes this work
def setAge(self, age):
self.age = age;
return self;
def setSSN(self, ssn):
self.ssn = ssn
return self
p = Person()
p.setName("Hunter")
.setAge(24)
.setSSN("111-22-3333")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question