英文:
Why does Python have some functions that are syntactically arranged as enclosed?
问题
我正在学习Python入门课程,刚刚学会了upper()
函数。然而,当我看到upper()
的正确用法不是upper(string)
,而是string.upper()
时,我感到很困惑。这种约定背后是否有特定的逻辑或原因?为什么其他函数不是这样的情况,括号除了标识它为函数之外还有什么作用?
英文:
I'm taking an Introduction to Python course, and I've just learned the upper()
function. However, I was really tripped up when I saw that the correct usage of upper()
was not upper(string)
, but rather, string.upper()
. Is there some particular logic or reasoning behind this convention? Why isn't this the case for other functions, and what purpose do the parentheses serve (aside from identifying it as a function)?
答案1
得分: 1
在Python中,(non-static)类方法的第一个参数是self
参数。如果您想要将其调用为upper(string)
,您很接近了。将其更改为str.upper(string)
。str
是定义这个upper()
方法的类型。
为什么其他函数不是这样的呢?
我不太确定您指的是什么。如果它不是一个类方法(参数列表不以self
开头),那么这不适用。
英文:
The first parameter of a (non-static) class method in Python is the self
parameter. If you want to call it as upper(string)
, you're close. Change it to str.upper(string)
. str
is the type that defines the upper()
method.
> Why isn't this the case for other functions
I'm not really sure what you're referring to here. If it's not a class method (parameter list doesn't start with self
), then this doesn't apply.
答案2
得分: 1
基本的设计原则是,在Python中,一切都是对象。不幸的是,这并不完全一致,但仍然是一个值得牢记的指导原则。
如果你有一个名为 instance
的对象实例,它具有许多属性,每个属性都可以表示为 instance.attribute
。如果属性是一个“可调用”的对象,你可以将其作为函数调用;所以 instance.method
返回一个函数,而带有括号的 instance.method()
返回该函数并调用它,实际上,在幕后会将 instance
作为第一个 self
参数传递。
有些情况下不遵循这个原则。一个情况是当某事涉及两个对象时。当你将两个东西相加时,显然需要将其中至少一个作为参数传递,而不是调用它的方法。因此,语法 3 + 4
简单地将两个项视为相等(尽管在内部实际上会变成 3.__add__(4)
)。另一个放弃这个模型的地方是一些常见的字符串或列表操作,比如 sorted()
(它本质上可以是 mylist.sorted()
,但实际上语法是 sorted(mylist)
)。
如果你刚刚开始学习,这可能有点超出你的理解范围。要理解这个表述,你需要对类有基本的了解。简而言之,
class Something:
def method(self):
print(f"hooray, I'm {self}")
将允许你创建具有 Something()
的新对象实例,每个实例都具有单独的身份和单独的私有属性集(在这里,例如,只有内置的 self
)。所以你可以做:
>>> a = Something()
>>> b = Something()
>>> a.method()
hooray, I'm <__main__.Something object at 0x107998e50>
>>> b.method()
hooray, I'm <__main__.Something object at 0x1079696d0>
(如果你仔细看,你可以看到 0x1079
后面的内容是唯一的。每个对象实例 a
和 b
都是内存中的新而唯一的实体。)
回到开头,3
和 4
都是具有许多方法的 int
对象,例如 __str__
(这是实际上在幕后调用的 str(3)
的方法;Python中有许多这样的“双下划线”方法,允许每种数据类型实现特定函数或操作的自定义行为)。你可以使用 dir
查看任何对象的属性(所以 dir(3)
将显示此 int
实例的属性)。同样,你的字符串是一个 str
对象实例,它具有一个名为 upper
的方法,该方法由内置的 str
类定义。
英文:
A basic design principle is that in Python, everything is an object. This is unfortunately not entirely consistent, but nevertheless a guiding principle which is good to keep in mind.
If you have an object instance named instance
, it has a number of attributes, each of which can be referred to as instance.attribute
. If the attribute is a "callable", you can invoke it as a function; so instance.method
returns a function, whereas instance.method()
with parentheses returns it and calls it, with instance
as the first self
arguments behind the scenes.
There are some cases where this principle is not observed. One is where something invokes two objects. When you add two things, you obviously need to pass in at least one of them as a parameter, rather than call a method on it. So the syntax 3 + 4
simply treats both terms as equals (though internally, this becomes 3.__add__(4)
actually). Another place where this model is abandoned is with some very common string or list operations, like sorted()
(which could be mylist.sorted()
but in fact the syntax is sorted(mylist)
instead).
If you are only just learning, this is probably a bit over your head. To understand this exposition, you need a basic understanding of classes. In very brief,
class Something:
def method(self):
print(f"hooray, I'm {self}")
will let you create new object instances with Something()
and each of them has a separate identity, and a separate collection of private attributes (here, for example, just the built-in self
). So you can do:
>>> a = Something()
>>> b = Something()
>>> a.method()
hooray, I'm <__main__.Something object at 0x107998e50>
>>> b.method()
hooray, I'm <__main__.Something object at 0x1079696d0>
(If you look closely, you can see that the stuff after 0x1079
is unique. Each object instance a
and b
is a new and unique entity in memory.)
Tying back to the beginning, 3
and 4
are int
objects with a number of methods, such as __str__
(which is what str(3)
actually calls behind the scene; there is a number of such "dunder" methods which allow each data type in Python to implement its own behavior for a particular function or operator). You can view the attributes of any object with dir
(so dir(3)
will show you the attributes of this int
instance). Similarly, your string
is a str
object instance which has a method called upper
which is defined by the built-in str
class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论