Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python str-函数、类或方法_Python - Fatal编程技术网

Python str-函数、类或方法

Python str-函数、类或方法,python,Python,我可以写'something'.strip()并得到某物,或者我可以键入str(8)并得到'8'。另外,type('a')返回str作为响应。str是一个函数(它将数值8转换为'8')还是一个类,它有strip()等方法?它是一个类,其构造函数可以获取任何Python对象,并尝试根据语言中内置的规则将其转换为字符串 它尝试做的第一件事是在传入的对象中调用\uuu str\uu方法。如果不存在,它将尝试调用方法。返回的内容将用作新生成的字符串 然而,str实际上是Python中的string类,因

我可以写
'something'.strip()
并得到
某物
,或者我可以键入
str(8)
并得到
'8'
。另外,
type('a')
返回
str
作为响应。
str
是一个函数(它将数值
8
转换为
'8'
)还是一个类,它有
strip()
等方法?

它是一个类,其构造函数可以获取任何Python对象,并尝试根据语言中内置的规则将其转换为字符串

它尝试做的第一件事是在传入的对象中调用
\uuu str\uu
方法。如果不存在,它将尝试调用
方法。返回的内容将用作新生成的字符串


然而,str实际上是Python中的string类,因为所有的string方法都是定义的

它是一个类,其构造函数可以获取任何Python对象,并尝试根据语言内置的规则将其转换为字符串

它尝试做的第一件事是在传入的对象中调用
\uuu str\uu
方法。如果不存在,它将尝试调用
方法。返回的内容将用作新生成的字符串


然而,str实际上是Python中的string类,因为所有的string方法都是定义的

str
是一个类。如图所示

正在创建类型为
str
的对象。它调用
str
的构造函数。它依次调用objects
\uuuu str\uuu
函数

正如评论中所建议的,您可以使用
type
关键字更仔细地查看发生了什么:

type(str)             # <class 'type'>
type(str())           # <class 'str'>
type('Hello, World!') # <class 'str'>
type(8)               # <class 'int'>
type(str(8))          # <class 'str'>
type(str().strip)     # <class 'builtin_function_or_method'>
type(str)#
类型(str())#
键入(“你好,世界!”)#
第(8)类
类型(str(8))#
类型(str().strip)#

str
是一个类。如图所示

正在创建类型为
str
的对象。它调用
str
的构造函数。它依次调用objects
\uuuu str\uuu
函数

正如评论中所建议的,您可以使用
type
关键字更仔细地查看发生了什么:

type(str)             # <class 'type'>
type(str())           # <class 'str'>
type('Hello, World!') # <class 'str'>
type(8)               # <class 'int'>
type(str(8))          # <class 'str'>
type(str().strip)     # <class 'builtin_function_or_method'>
type(str)#
类型(str())#
键入(“你好,世界!”)#
第(8)类
类型(str(8))#
类型(str().strip)#

str
是一个类
str(8)
调用对象构造函数(不是函数,而是类似函数)并返回
str
类的实例。继续尝试,编写
type(str())
type(str().strip)
,同时
type(str)
-表示它是一个类。描述类的文档所引用的文档。文档的内置函数部分澄清了
。。内置在..
中的函数和类型感谢您的清晰解释;我真的很感激<代码>类型(str().strip)
响应
内置函数或方法
str
是一个类
str(8)
调用对象构造函数(不是函数,而是类似函数)并返回
str
类的实例。继续尝试,编写
type(str())
type(str().strip)
,同时
type(str)
-表示它是一个类。描述类的文档所引用的文档。文档的内置函数部分澄清了
。。内置在..
中的函数和类型感谢您的清晰解释;我真的很感激<代码>类型(str().strip)
内置函数或方法响应。