python 在哪里存储 str 的原始值
where does python store origin value for str
我是 python 的新手,我有一个简单的问题。
假设我创建了一个 str 的子类来将所有内容更改为 'test':
class Mystr(str):
def __str__(self):
return 'test'
def __repr__(self):
return 'test'
>>> mystr = Mystr(12345)
>>> print(mystr)
test
>>>
>>> print(mystr + 'test')
12345test
我想要的只是'test',但是
python 在哪里存储原始值 '12345' ?
我到处都找不到。
>>> dir(mystr)
['__add__', '__class__', '__contains__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
查了之后,居然在getnewargs
中找到了
>>> mystr.__getnewargs__()
('12345',)
然后我将其更改为:
class Mystr(str):
def __str__(self):
return 'test'
def __repr__(self):
return 'test'
def __getnewargs__(self):
return 'test'
>>> mystr = Mystr(12345)
>>> print(mystr)
test
>>> mystr.__getnewargs__()
'test'
>>> print(mystr + 'test')
12345test
为什么12345还在?
python 在哪里存储原始值 '12345' ?
Python 3.6.1(v3.6.1:69c0db5,2017 年 3 月 21 日,18:41:36)[MSC v.1900 64 位 (AMD64)] on win32
它存储在self
中。 self
是你的方法附加的对象,也就是字符串。
每当您将参数传递给 class 时,都会执行 class 的本地内置函数 __getnewargs__。然后该函数将所有传递给 class 的变量初始化为参数并将它们存储在一个元组中。
现在,当您尝试连接 class 对象时,在您的例子 mystr 中,元组包含 ('12345',) returns 它唯一的参数 '12345' 然后与字符串连接并打印为 '12345test'.
如果你尝试以这种方式打印它print(mystr + ' anything')
,它会打印'12345 anything'
更多信息请查看
this screenshot
我是 python 的新手,我有一个简单的问题。
假设我创建了一个 str 的子类来将所有内容更改为 'test':
class Mystr(str):
def __str__(self):
return 'test'
def __repr__(self):
return 'test'
>>> mystr = Mystr(12345)
>>> print(mystr)
test
>>>
>>> print(mystr + 'test')
12345test
我想要的只是'test',但是 python 在哪里存储原始值 '12345' ? 我到处都找不到。
>>> dir(mystr)
['__add__', '__class__', '__contains__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
查了之后,居然在getnewargs
中找到了>>> mystr.__getnewargs__()
('12345',)
然后我将其更改为:
class Mystr(str):
def __str__(self):
return 'test'
def __repr__(self):
return 'test'
def __getnewargs__(self):
return 'test'
>>> mystr = Mystr(12345)
>>> print(mystr)
test
>>> mystr.__getnewargs__()
'test'
>>> print(mystr + 'test')
12345test
为什么12345还在? python 在哪里存储原始值 '12345' ?
Python 3.6.1(v3.6.1:69c0db5,2017 年 3 月 21 日,18:41:36)[MSC v.1900 64 位 (AMD64)] on win32
它存储在self
中。 self
是你的方法附加的对象,也就是字符串。
每当您将参数传递给 class 时,都会执行 class 的本地内置函数 __getnewargs__。然后该函数将所有传递给 class 的变量初始化为参数并将它们存储在一个元组中。
现在,当您尝试连接 class 对象时,在您的例子 mystr 中,元组包含 ('12345',) returns 它唯一的参数 '12345' 然后与字符串连接并打印为 '12345test'.
如果你尝试以这种方式打印它print(mystr + ' anything')
,它会打印'12345 anything'
更多信息请查看 this screenshot