class myClass:
def __init__(self, text):
self.text = text
def printText(text):
more_text = "Why so "
return more_text + text
上記は、私がウェブページからデータを抽出するために構築しているコードのオーバーライドされたバージョンです。私はこのようなtemp.pyコードを実行しています。クラス内で 'str'オブジェクトと 'instance'オブジェクトを連結できないのはなぜですか?
>>> 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'オブジェクトの連結に関する問題の多くの例があります。
OPTION 1:
私は、次の方法を試してみましたINPUT
class myClass:
def __init__(self, str(text)):
self.text = text
def printText(text):
more_text = "Why so "
return more_text + text
しかし、私は取得AS のinit DURING文字列にテキストを変換...
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:テキストを変換するSTRING のinit STEP
class myClass:
def __init__(self, text):
self.text = str(text)
def printText(text):
more_text = "Why so "
return more_text + text
DURINGしかし、私は...
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
は、誰かが私に問題に良い解決策をお願いすることができますか?私のオリジナルコードでは、クラス内の2つの文字列オブジェクトを連結してWebページリンクを作成することを意図しています。どんな提案もありがとうございます。
'' printText'は*あなた*メソッドの最初の引数である中text'が、それはこのように、それはないだろう、暗黙的にインスタンスを渡す*であるため、作業。 –