This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 3 years ago.
What is the self parameter? Because I looked at the documentation and it only mentions x and y as parameters.
In Python, the declaration of any method of a class has the parameter self, which is a reference to the current instance of the class. That's what allows you to access the variables and methods in this scope.
More info here.
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:
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:
python: class override "is" behavior
(4 answers)
Is it possible to get a list of keywords in Python?
(7 answers)
Closed 4 years ago.
I know that in python we can use __eq__ method to check equality and __iter__ when using in keyword. Is there any method for is keyword?
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?