In Python, what's the difference between some_string.lower () and str.lower (some_string)

I am confused with the built-in method in Python. For example, what are some_string.lower () and str.lower (some_string)?

+4
source share
3 answers

str is the class name of all strings in Python. str.lower is one of his methods.

If you call lower in one of your instances (for example, 'ABC'.lower() ), you call the bound method, which automatically sends the called object as the first argument (usually called self ).

If you call lower in the class itself (i.e. use str.lower() ), you call the unbound method , which does not automatically provide the self argument. Therefore, you must specify an object in order to act independently.

If all this seems difficult to understand, it will be easier when you consider how methods are defined in classes. Let them say that we create our own very simple class that represents a point (X, Y coordinate in space). And it has a show() method to print the dot.

 class Point: """This is the constructor of Point""" def __init__(self, x, y): # Save arguments as field of our class instance (self) self.x = x self.y = y def show(self): print self.x, self.y # We now create an instance of Point: p = Point(1.0, 2.0) # We now show p by calling a bound method p.show() 

Please note that we did not need to specify the argument self (therefore p.show () was called without arguments). In fact, the previous call was more or less equivalent to this:

 Point.show(p) 

They are not quite equivalent, but this is a more advanced topic. One of the simplest cases when they will not be equivalent is if you change the p.show value after creating the object, for example:

 p.show = 4 

Now p.show() will not even compile, since p.show is no longer a function, but an integer! However, Point.show(p) will remain unchanged since we modified the show attribute in an instance of class ( p ), and not in the class itself ( Point ).

+5
source

The first call to the binding method, the second is the call to the unbound method.

Consider writing a method like this:

 class str: ... def lower(self): # And so on 

The first argument is self. If you use this method from an instance (for example, some_string.lower() , the instance is automatically passed as the first argument to the method (as self ).

However, if you call it from a class (as an unrelated method), e..g str.lower(some_string) , there is no instance that is automatically passed as the first argument. Thus, instead of some_string is passed as self and everything that would be done with the instance in the first case is done using some_string .

However, you never need to use an unrelated version, since any line will have a lower() method that you can call. The preferred style is to use some_string.lower() .

+3
source

Very easy if you can try this code:

 "รŸ".lower() 

you can see this output:

 'รŸ' 

But if you try to use casefold () in ascii characters, you will see that:

 "รŸ".casefold() 'ss' 

This is because casefold converts all ascii characters to 'ss'

-1
source

All Articles