This question already has answers here:
Accessing attributes on literals work on all types, but not `int`; why? [duplicate]
(4 answers)
Closed 6 years ago.
I just want to understand the behavior of method call on integer object.
I can't call __add__ method directly on integer.
5.__add__(5)
This gives me: SyntaxError: invalid syntax
However, doing num = 5 and call num.__add__(5) works fine.
How assigning name to an integer object makes difference?
it is just syntax. The Parser reads:
"5." -> a float :)
"__add__" -> why that? I don't understand :(
Related
This question already has answers here:
str - Function, Class or Method
(2 answers)
int('123') in python - is it a function call or constructor call of 'int' class?
(2 answers)
Closed last month.
In Python, when we call str(<something>), are we calling the function, or are we calling the class constructor (the class being str)? Similarly, int(), complex() etc.
This question already has answers here:
What does -> mean in Python function definitions?
(11 answers)
Python3 function definition, arrow and colon [duplicate]
(3 answers)
What does the -> (dash-greater-than arrow symbol) mean in a Python method signature? [duplicate]
(1 answer)
Closed 9 months ago.
I'm analyzing some old code that I've inherited, and I have a question about the use of "self" and "None" keywords, specifically in the following example:
def run(self) -> None:
I understand that the self keyword is similar to the "this" keyword in C++ in that, in conjunction with the dot operator, it allows us to access the attributes and methods of the class in question. What I'm really interested in is the use of "-> None" in the declaration of the method named "run." Is this in PEP 8 because I can't find an example. I'm using Python 3.7, in case that matters.
What is the purpose of writing a method in this manner? What does "-> None" do?
They're called type hints, and they enable annotating the types of the parameters and return types of functions.
https://peps.python.org/pep-0484/
This question already has answers here:
int object not callable error in python [closed]
(2 answers)
Closed 2 years ago.
I have researched this error and know that it has something to do with the naming of my variables, but I don't see any variables in my code that are "magic words". What am I missing? Thanks!
def shrink_inv (p,r,n,t):
return p(1+r/n)**n*t
shrink_inv(10,-0.1,1,1)
return p(1+r/n)**n*t
You are missing an operator after p - it's currently interpreted as a function call.
This question already has answers here:
Why isn't the 'len' function inherited by dictionaries and lists in Python
(7 answers)
Why does Python code use len() function instead of a length method?
(7 answers)
In Python, when should I use a function instead of a method?
(5 answers)
Difference between len() and .__len__()?
(5 answers)
Closed 4 years ago.
In Python, why are some built-in functions called using brackets with the method name before the object, e.g.
print("foobar")
bool("foobar")
...
While others are built-in method attributes, called with a dot behind the object, e.g.
"foobar".capitalize()
Specifically I'm interested to learn if there is a general principle behind this instead of just common practice and memorization. In cases where you can't quite remember whether it was capitalize("foobar") or "foobar".capitalize(), how do you know?
This question already has answers here:
Bare asterisk in function parameters?
(6 answers)
What is the purpose of class methods?
(18 answers)
Closed 5 years ago.
Looking into the Python documentation, I came across the following page:
https://docs.python.org/3/library/stdtypes.html#int.from_bytes
Here, the third argument of int.from_bytes() is *. What does * represent and why can it be ignored in the examples?
Also, the fromkeys(seq[, value]) method is said to be a classmethod, but what exactly does this mean? What class does this method belong to?