私はPython 3.xで "Facebook"を作成するプロジェクトに取り組んでいます。私が現在取り組んでいる部分は、str関数を使用して、異なる行に文字列を返すことです。__str__を使用して、異なる数の異なる文字列を返すか?
私はこのために使用していたコードは次のとおりです。
class Status:
likers = []
commentObjs = []
def __init__(self, statusPoster, statusMsg, likers, commentObjs):
self.statuser = statusPoster
self.status = statusMsg
self.likers = likers
self.commentObjs = commentObjs
と
def __str__(self):
return '%s: %s \n"hello"' %(self.statuser,self.status)
__repr__= __str__
私はに実行しています問題はlikersの可変数の可変数があり得ることですcommentObjs。それは、端末に返す
likers = ["Spongebob"]
commentObjs = ["Spongebob: You should watch the Spongebob movie!"]
:
は、私が何をして一つの値だけのような存在であるかのように、それを作るために実装する必要があります
Brad Pitt will watch a movie today!
Spongebob likes this.
Spongebob: You should watch The Spongebob movie!
しかし、複数ある場合
likers = ["Spongebob","Harry Potter"]
commentObjs = ["Spongebob: You should watch the Spongebob movie!","Brad Pitt: How about nah?"]
が返す:のような各リストの値、
Brad Pitt will watch a movie today!
Spongebob, Harry Potter likes this.
Spongebob: You should watch The Spongebob movie!
Brad Pitt: Nah, I will probably watch Mr and Mrs. Smith.
私はおそらくこれがループとlen(likers)
ためで何かだろう行うことを考えることができる唯一の方法が、それでも名前の定数値を戻しながら、私はそれを行うことができるだろうかわからないし、状態。
私は '__repr__'としてこれを使用しないことを強くお勧めします。 – user2357112