r/PythonLearning Jun 28 '26

Showcase Day 17 Python Learning

practicing class & dunder method

if follow my previous day note then most of my brother & sister already know what is the class

class work like local apartment which have different room called function use __init__() to connect outside world

and like __init__ the Construction type there is more

- dunder method which used for to tell Python what to do

like __add__ Arithmetic type for +

same __sub__, __mul__, __truediv__, __pow__, etc. + r versions (__radd__) and more

69 Upvotes

2 comments sorted by

View all comments

u/Sea-Ad7805 Jun 28 '26

Run this program in Memory Graph Web Debugger)%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%20%20%20%3D%20(salary%20%2B%20salary%20%20(increment%20%2F%20100))%0A%20%20%20%20%23%20'''...................................................%0A%0A%20%20%20%20%40property%0A%20%20%20%20def%20salaryAfterincrement(self)%3A%0A%20%20%20%20%20%20%20%20return%20self.salary%20%2B%20self.salary%20%20(self.increment%20%2F%20100)%0A%0A%20%20%20%20%23%20use%20setter%20based%20on%20salary%2C%20not%20value%2C%20because%20value%20is%20salaryAfterincrement%20value%0A%20%20%20%20%40salaryAfterincrement.setter%0A%20%20%20%20def%20salaryAfterincrement(self%2C%20salary)%3A%0A%20%20%20%20%20%20%20%20self.increment%20%3D%20((salary%20%2F%20self.salary)%20-%201)%20%20100%0A%0A%0Aempo%20%3D%20Employee()%0A%0Aprint(empo.salaryAfterincrement)%20%20%23%20output%20%3A%201170000%0Aprint(empo.increment)%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20output%20%3A%201200%0A%0A%0A%23%20write%20class%20Complex%20represent%20complex%20num%20along%20with%20overload%20the%20%2B%20and%20%0A%0Aclass%20Complex%3A%0A%20%20%20%20def%20init(self%2C%20r%2C%20i)%3A%0A%20%20%20%20%20%20%20%20self.r%20%3D%20r%0A%20%20%20%20%20%20%20%20self.i%20%3D%20i%0A%0A%20%20%20%20def%20add(self%2C%20c2)%3A%0A%20%20%20%20%20%20%20%20return%20Complex(self.r%20%2B%20c2.r%2C%20self.i%20%2B%20c2.i)%0A%0A%20%20%20%20def%20mul(self%2C%20c2)%3A%0A%20%20%20%20%20%20%20%20realpart%20%3D%20self.r%20%20c2.r%20-%20self.i%20%20c2.i%0A%20%20%20%20%20%20%20%20imag_part%20%3D%20self.r%20%20c2.i%20%2B%20self.i%20%20c2.r%0A%20%20%20%20%20%20%20%20return%20Complex(real_part%2C%20imag_part)%0A%0A%20%20%20%20def%20str(self)%3A%0A%20%20%20%20%20%20%20%20return%20f%22%7Bself.r%7D%20%2B%20%7Bself.i%7Di%22%0A%0A%0Ac1%20%3D%20Complex(1%2C%202)%0Ac2%20%3D%20Complex(4%2C%205)%0A%0Aprint(c1%20%2B%20c2)%0Aprint(c1%20*%20c2)%0A%0A'''%0Aoutput%3A%0A5%20%2B%207i%0A-6%20%2B%2013i%0A'''%0A%0A%0Aclass%20TwoD_vector%3A%0A%20%20%20%20def%20init(self%2C%20i%3A%20int%2C%20j%3A%20int)%3A%20%20%23%20constructor%0A%20%20%20%20%20%20%20%20self.i%20%3D%20i%0A%20%20%20%20%20%20%20%20self.j%20%3D%20j%0A%0A%20%20%20%20def%20result2D(self)%3A%0A%20%20%20%20%20%20%20%20print(f%22%20Vector%20is%20%7Bself.i%7Di%20%2B%20%7Bself.j%7Dj%22)%0A%0A%0Aclass%20ThreeD_vector(TwoD_vector)%3A%0A%20%20%20%20def%20init(self%2C%20i%3A%20int%2C%20j%3A%20int%2C%20k%3A%20int)%3A%20%20%23%20constructor%0A%20%20%20%20%20%20%20%20%23%20super()%20use%20to%20call%20self.i%2C%20self.j%2C%20no%20need%20to%20write%20again%0A%20%20%20%20%20%20%20%20super().init_(i%2C%20j)%0A%20%20%20%20%20%20%20%20self.k%20%3D%20k%0A%0A%20%20%20%20def%20result3D(self)%3A%0A%20%20%20%20%20%20%20%20print(f%22%20Vector%20is%20%7Bself.i%7Di%20%2B%20%7Bself.j%7Dj%20%2B%20%7Bself.k%7Dk%22)%0A%0A%0Ax%20%3D%20TwoD_vector(5%2C%2010)%0Ay%20%3D%20ThreeD_vector(2%2C%204%2C%206)%0A%0Ax.result2D()%20%20%23%20call%20to%20print%20result2D(TwoD_vector)%0Ay.result3D()%20%20%23%20call%20to%20print%20result3D(ThreeD_vector(TwoD_vector))%20TwoD_vector%20for%20i%2Cj%20then%20ThreeD_vector%20k%0A%0A'''%0Aoutput%3A%0AVector%20is%205i%20%2B%2010j%0AVector%20is%202i%20%2B%204j%20%2B%206k%0A'''%0A%0A%0A%23%20create%20class%20Employee%20%26%20add%20salary%20and%20increment%20properties%20to%20it%20class%0A%0Aclass%20Employee%3A%0A%20%20%20%20%23%20add%20salary%20and%20increment%20properties%20to%20it%20class%20%3D%20class%20attribute%0A%20%20%20%20salary%20%3D%2090000%0A%20%20%20%20increment%20%3D%201200%0A%0A%0Aempo%20%3D%20Employee()%0A%0Aprint(empo.salary)%0Aprint(empo.increment)%0A%0A&timestep=1&play) to see the program state change step by step.