【问题标题】:Why can I not concatenate 'str' and 'instance' objects inside a class?为什么我不能在一个类中连接“str”和“instance”对象?
【发布时间】:2017-07-22 02:51:07
【问题描述】:
class myClass:

    def __init__(self, text):
            self.text = text

    def printText(text):
            more_text = "Why so "

            return more_text + text

以上是我为从网页中提取数据而构建的代码的过度简化版本。我正在运行这样的 temp.py 代码。

>>> from temp import myClass
>>> text = "serious?"
>>> joker_says = myClass(text)
>>>
>>> print joker_says.printText()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "temp.py", line 9, in printText
    return more_text + text
TypeError: cannot concatenate 'str' and 'instance' objects

我在 Stack Overflow 中看到了很多“str”和“instance”对象连接问题的例子。

我尝试了以下方法:

选项 1:在 init 作为输入时将文本转换为字符串

class myClass:

    def __init__(self, str(text)):
            self.text = text

    def printText(text):
            more_text = "Why so "

            return more_text + text

但我明白了……

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "temp.py", line 3
    def __init__(self, str(text)):
                      ^
SyntaxError: invalid syntax

== == == == == ==

选项 2:在 init 步骤中将文本转换为字符串

class myClass:

    def __init__(self, text):
            self.text = str(text)

    def printText(text):
            more_text = "Why so "

            return more_text + text

但是我明白了……

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "temp.py", line 9, in printText
    return more_text + text
TypeError: cannot concatenate 'str' and 'instance' objects

有人可以给我一个很好的解决问题的方法吗?请注意,在我的原始代码中,我的意图是在类中连接两个字符串对象以创建网页链接。任何建议将不胜感激。

【问题讨论】:

  • 因为printText 中的text 是您的方法 的第一个参数,所以它*隐式传递给了实例,因此它不起作用。

标签: python string class instance init


【解决方案1】:

这里有几个问题:

在您为对象创建的每个函数中,self 必须作为第一个参数包含在内。

使用你的第二个例子:

class myClass:

    def __init__(self, text):
            self.text = str(text)

    def printText(self, text):
            more_text = "Why so "

            return more_text + text

然后,你创建一个你的类的实例,你可以访问函数printText

joker = myClass("This is some text")
print(joker.text) # This prints: "This is some text"
print(joker.printText("serious?")) # This prints "Why so serious?"

如果您想使用与初始化文本相同的文本,则需要引用它,而不是作为新参数text,作为类的属性,如下所示:

class myClass:

    def __init__(self, text):
            self.text = str(text)

    def printText(self):
            more_text = "Why so "

            return more_text + self.text

那么,如果你想参考上面的:

joker = myClass("serious?")
print(joker.text) # This prints: "serious?"
print(joker.printText()) # This prints "Why so serious?"

【讨论】:

  • 谢谢!我觉得我懂了。但是,我认为正如您所指出的,def printText 中应该有一个 self,而不是 text。这样 printData 就不会寻找两个输入。感谢您的快速响应!
【解决方案2】:

你面临的主要问题是:

def printText(text):

您收到此错误的原因是,作为实例方法,声明希望您将self(实例对象)作为第一个参数传递。您现在正在传递text,它被用作self(实例)。这就是您收到错误的原因,因为最终您实际上在做的是 尝试添加带有实例的字符串。

所以,知道隐式传递给 printText 的第一个参数是实例,并且查看您的方法内部,您实际上想要在您的 printText 方法中引用 self.text .但是,传递给printText实例 实际上称为text。这可能非常令人困惑。

因此,按照建议的命名法,您应该将您的实例参数命名为“预期的”,self。

考虑到这一点,您想要引用的text 现在可以引用为self.text

这可以通过修复你的代码来显示:

def printText(self):
        more_text = "Why so "

        return more_text + self.text

【讨论】:

  • 注意,OP 使用的是print joker_says.printText(),所以会抛出一个TypeError,因为它会丢失一个参数...
  • @juanpa.arrivillaga 拍摄。你让我意识到真正的问题是什么。感谢您提出这个问题。
  • @juanpa.arrivillaga 谢谢。我修好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多