gpt4 book ai didi

python - 当人们在谈论正则表达式时说对象时,他们是什么意思(Python)

转载 作者:行者123 更新时间:2023-12-01 09:20:03 25 4
gpt4 key购买 nike

我目前正在学习 Python 类(class),我的导师所说的一些话让我感到困惑。这是我们编写的代码。

import re
message = "Call me at 415-555-1011 tommorow or at"\
" 415-555-9999 for my office line"
phoneNumRegX = re.compile(r"\d\d\d-\d\d\d-\d\d\d\d")
mo = phoneNumRegX.findall(message)

他说我们正在创建一个正则表达式对象,当我们赋值的时候phoneNumRegX 到 re.compile(r"\d\d\d-\d\d\d-\d\d\d\d") 但这是什么意思呢?我听说过“对象”这个词,它在 Python 中指代不同的事物。我想知道他的意思。有人能解释一下他在说什么吗?

最佳答案

regular expression object”正在描述SRE_Pattern的实例类(class)。此类定义了用于执行各种操作的方法,例如 findall()您的示例中的方法。

类、对象和方法在 Classes section of the Python tutorial 中进行了一般讨论。 。介绍性段落很好地总结了这一点:

Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.

关于python - 当人们在谈论正则表达式时说对象时,他们是什么意思(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50867620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com