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.
Related
This question already has answers here:
What's ```__new__``` by default in Python 3?
(2 answers)
__new__ and __init__ in Python
(1 answer)
Why not just __new__ and not __new__ and __init__ in python?
(2 answers)
Closed 7 months ago.
in the class concept of python, it states that __init __ initializes the class, which I understood as setting defaults to the class.
However, I bumped into a new concept called __new __, which states that it is used to control the creation of the class.
I have seen several explanations saying that __new __ also returns an instance, but I am also confused about what that is returning and to where.
Can someone clarify these points, please?
This question already has answers here:
How to implement built-in sum() of the class?
(2 answers)
Define "sum" for a class using non-associative addition
(2 answers)
Closed 9 months ago.
Is there any way I can over write the sum() method to act differently on a list?
I expected I would be able to find a dunder method that would allow me to define my own implementation, but I couldn't.
That's how I'd imagine the implementation
class MyDummyList(list):
def __sum__(self): -> str:
return 'a'
x = MyDummyList()
print(sum(x)) -> # prints 'a'
Obviously this breaks as there is no dunder method __sum__ but I am somewhat surprised that there is no such a way to do that using dunder methods as sum is a quite common operation on iterables.
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?
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 :(