我是Python新手,嘗試通過創建地址簿進行實驗。我懷疑我的displayPerson函數有問題。我不能同時返回多個變量嗎?如何從Python函數中返回多個變量
def lowercasewrapper(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs).lower()
return wrapper
#Class with object attributes
class People():
numofpeeps = 0
listofpeeps = []
def __init__(self, name, age, phone, fblink):
self.name=name
self.age=age
self.phone=phone
self.fblink=fblink
People.numofpeeps += 1
People.listofpeeps.append(self.age)
@lowercasewrapper #calling the wrapper, to make all strings lowercase
def displayPerson(self):
return self.name, self.age, self.phone, self.fblink
george=People("gEORge", "5", "503-405-4021", "http://facebook.com/boobs")
dave=People("dave", "10", "971-863-3905", "http://boobs.com")
charlie=People("CHARLIE", "19", "823-405-2942", "http://boobs.com")
print george.displayPerson()
print "Total number of people: ", People.numofpeeps
print "List of ages: ", People.listofpeeps
error shows the following: File "example.py", line 54, in <module>
print george.displayPerson()
File "example.py", line 31, in wrapper
return func(*args, **kwargs).lower()
AttributeError: 'tuple' object has no attribute 'lower'
我稍微遵循這一點,但我不能得到它的工作。是複製並粘貼你的例子應該工作?我收到錯誤:未定義全局名稱initial_tuple。 – stephan
是的,它應該按原樣工作。如果它有幫助,它和@DonaldMiner的答案完全一樣(只是格式有點不同以幫助理解) - 可能值得嘗試一下。 – Mac
下方法從哪裏來?這只是內置於Python嗎? – stephan