r/PythonLearning • u/clcodingupdates • 9d ago
Python Quiz of the day
What will be the output of the following Python Code?
a = 5
print(a.mul(3).add(2))
7
Upvotes
r/PythonLearning • u/clcodingupdates • 9d ago
What will be the output of the following Python Code?
a = 5
print(a.mul(3).add(2))
•
u/Sea-Ad7805 9d ago
Try:
by clicking Memory Graph Web Debugger.
2
u/Money_Ad_5277 9d ago
Okay, result is 28, because
a.__mul__(3).__add__(3)works just fine, but text between __ is converted to italic, because markdown1
u/Money_Ad_5277 9d ago edited 9d ago
Error? int object doesn't have attribute mul?1
u/johlae 9d ago
AttributeError
1
u/[deleted] 9d ago
This will throw an error!
intobject has no attributemul. In Python,a.mul(3)does not work like that.The correct way would be:
print(a * 3 + 2)Output: 17Nice trick question though, beginners will definitely get confused on this one.nice try
1
u/Orgasml 9d ago
how about just: print(a*3+2)
1
u/captain_kringel 8d ago
Error
1
u/RNG_HatesMe 8d ago
All, the answer is 17, if the question is actually rendered correctly. Unfortunately, the double underscores are getting converted to italics font.
These are dunder (double underscore methods), which are references to functions that are *internal* to a class. The dunder add() and mul() methods are actually what + and * call, but you can override them in your class is you'd like them to behave differently.
1
u/MasterpieceBusy7220 5d ago
One day this bot will figure out how to use code blocks