用这个字符串操作脚本来理解python类,它给了m

2024-10-08 18:22:50 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在python2.7中有以下代码。与主.py和一个字符串处理.py包含类

在我的主.py我有

import stringmanipulation  

testvalue = "[323232 , 232323]"
testvalue.stringmanipulation.modify()

在我的字符串处理.py我有

class modify (object):
    def __init__(self):
         extract = self.split('[', 1)[1].split(']')[0]
         extract.split(", ")
         self.x = extract[0]
         self.y = extract[1]

现在我得到的错误是

testvalue.stringmanipulation.modify()
AttributeError: 'str' object has no attribute 'stringmanipulation'

如果我用另一种方法

newvariable = stringmanipulation.modify(testvalue)

这个代码很好用


Tags: 字符串代码pyimportselfobjectinitdef

热门问题