In Python, why is contains() a hidden method on str object? [duplicate] - python

This question already has answers here:
Why does Python use 'magic methods'?
(8 answers)
Closed 6 years ago.
Often I check if a substring is present in a string. In Python I could do that either using the __contains__() method or using the in operator as shown below:
Pattern 1:
string1 = r'c:\users\username\documents\code\crime_data.csv'
print(string1.__contains__('username'))
Pattern 2:
print('username' in string1)
Both work well for me, but I would like to know why is __contains__ hidden method and if pattern 2 preferred over 1, if so why? Other programming languages like C# do have a String.contains() method in their standard library.

The in operator can be overloaded by redefining the method __contains__.
This is no different from e.g. __add__ (+), __mul__ (*), etc. So the point is not to hide the method, but to make it fit the convention for 'magic methods', used to overload operators.

Related

Overwrite sum behavior on a custom class inhering from an iterable (list) [duplicate]

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.

Use of None and self keywords in method construction [duplicate]

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/

Python dot notation method attributes VS other methods [duplicate]

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?

Overloading = operator in Python [duplicate]

This question already has an answer here:
Is it possible to override the assignment ('=') operator in Python?
(1 answer)
Closed 5 years ago.
I found out yesterday that it is possible to override operators in python, so after a bit of googling i found out how, but i could not find any way of overloading the "=" sign. There is __set__() but as i understand it, it overloads the sign for attributes in the object, and not for the object itself.
What i want to accomplish is this:
F = Foo(1)
G = Foo(2)
F = G #overloaded =
So is there a way of overloading "=" for an object in python? (and what is that function called)
You cannot overload =(assign) operator.

Why does Python require the "self" parameter? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
python ‘self’ explained
Why do you need explicitly have the “self” argument into a Python method?
Why does Python require the "self" parameter for methods?
For example def method_abc(self, arg1)
And is there ever a date that the need for it will be removed?
Python gives you the option of naming it something other than self, even though the standard is to name it self. Just as it gives you the option of using tabs for indents, even though the standard is to use spaces.
In other words, it's not just "assumed" because...
To give you naming flexibility
To make it clearer that something will be passed self (or not).

Categories

Resources