简体   繁体   English

在Python中,some_string.lower()和str.lower(some_string)之间有什么区别

[英]In Python, what's difference between some_string.lower() and str.lower(some_string)

I am confused with built-in method in Python. 我对Python中的内置方法感到困惑。 For instance, what is the some_string.lower() and str.lower(some_string) and how are they different? 例如, some_string.lower()str.lower(some_string)是什么,它们有什么不同?

str is the name of the class of all strings in Python. str是Python中所有字符串的类的名称。 str.lower is one of its methods. str.lower是它的方法之一。

If you call lower on one of its instances (eg 'ABC'.lower() ), you call a bound method , which automatically sends the called object as the first argument (usually called self ). 如果在其中一个实例上调用lower (例如'ABC'.lower() ),则调用绑定方法 ,该方法自动将被调用对象作为第一个参数(通常称为self )发送。

If you call lower on the class itself (ie you use str.lower() ), then you call an unbound method , which doesn't provide the self argument automatically. 如果在类本身上调用lower (即使用str.lower() ),则调用未绑定的方法 ,该方法不会自动提供self参数。 Therefore, you have to specify the object to act upon by yourself. 因此,您必须指定要自行处理的对象。

If all of this seems hard to understand, it will be easier when you consider how methods are defined in the classes. 如果所有这些看起来都难以理解,那么在考虑如何在类中定义方法时会更容易。 Let's say we create our own very simple class, which represents a point (X,Y coordinate in space). 假设我们创建了一个非常简单的类,它代表一个点(空间中的X,Y坐标)。 And has a show() method to print the point. 并且有一个show()方法来打印这个点。

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()

Note that we didn't have to specify the self argument (so p.show() was called with no arguments). 请注意,我们不必指定self参数(因此调用p.show()时没有参数)。 In reality, the previous call was more or less equivalent to this: 实际上,之前的调用或多或少与此相同:

Point.show(p)

They're not entirely equivalent, but that's a more advanced topic. 它们并不完全等同,但这是一个更高级的主题。 One of the simplest cases when they will not be equivalent is if you change the value of p.show after creating the object, for instance: 如果在创建对象后更改p.show的值,则最简单的一种情况是,例如:

p.show = 4

Now, p.show() won't even compile, since p.show is not a function anymore, but an integer! 现在, p.show()甚至不会编译,因为p.show不再是一个函数,而是一个整数! However, Point.show(p) would still be unchanged, since we only modified the show attribute in the class instance ( p ) and not in the class itself ( Point ). 但是, Point.show(p)仍然没有改变,因为我们只修改了类实例( p )中的show属性而没有修改类本身( Point )中的show属性。

The first is a bound method call and the second is an unbound method call. 第一个是绑定方法调用,第二个是未绑定方法调用。

Think about how you'd write 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 (eg some_string.lower() , the instance automatically gets passed as the first argument to the method (as self ). 如果从实例中使用此方法(例如some_string.lower() ,则实例会自动作为方法的第一个参数传递(作为self )。

However, if you call it from the class (as an unbound method), e..g str.lower(some_string) , there is no instance to automatically pass as the first argument. 但是,如果从类中调用它(作为未绑定方法), str.lower(some_string) ,则没有实例作为第一个参数自动传递。 So, instead, some_string gets passed as self and whatever would've been done with the instance in the first case gets done with some_string . 因此,相反, some_string作为self传递,并且在第一种情况下使用some_string完成了对实例的任何操作。

It's never really necessary to use the unbound version, however, as any string will have a lower() method that you can call. 但是,从来没有必要使用未绑定版本,因为任何字符串都有一个可以调用的lower()方法。 The preferred style is to use some_string.lower() . 首选的样式是使用some_string.lower()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM