Python method chaining ⛓️

preview_player
Показать описание
Python method chaining tutorial explained

#python #method #chaining

# method chaining = calling multiple methods sequentially
# each call performs an action on the same object and returns self

class Car:

def turn_on(self):
print("You start the engine")
return self

def drive(self):
print("You drive the car")
return self

def brake(self):
print("You step on the brakes")
return self

def turn_off(self):
print("You turn off the engine")
return self

car = Car()

.drive()\
.brake()\
.turn_off()

Bro Code merch store 👟 :
===========================================================
===========================================================
Рекомендации по теме
Комментарии
Автор

tldr

# method chaining = calling multiple methods sequentially
# each call performs an action on the same object and returns self

class Car:

def turn_on(self):
print("You start the engine")
return self

def drive(self):
print("You drive the car")
return self

def brake(self):
print("You step on the brakes")
return self

def turn_off(self):
print("You turn off the engine")
return self


car = Car()

# car.turn_on().drive()
# car.brake().turn_off()
#

car.turn_on()\
.drive()\
.brake()\
.turn_off()

BroCodez
Автор

bro you really explain in a simple and fast way. Thanks

Franco-ssbu
Автор

You are just a cannon keep making videos champion👍

דודפתחיהקסטוריאנו
Автор

Incredible video making dude, including the tldr comment. You're truly awesome!!

Freef_
Автор

thanks so much! you sound like a robot and I love it!

oscarjoselopezdiaz
Автор

so should you always use return self in object class functions or only if plan on chaining those functions, are there situations where the return statement can cause issues? im coming from java and return statement is usually only used for data type methods, just trying to get a deeper grasp on the concept. Thanks for vids!

theosouza
Автор

Hello! Very nice video indeed.
I wanted to ask how would one perform a similar chain but with addition. For example a class Calculator() with a method add_number(someValue).
Then one would use:
myCalc = Calculator()


For some reason in my attempts, the result from print prints only the address of the object rather then the result.

vladisan
Автор

Are methods always read from left to right? Or can the order of the method chain be changed?

rodneychan
Автор

Awesome video! Could you explain a bit more why are we returning self?

nrth
Автор

I'm the 999th person who give a like!

Gokeniz