gpt4 book ai didi

python - 如何从 Python 函数返回多个变量

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:42 25 4
gpt4 key购买 nike

我是 Python 的新手,正在尝试通过创建地址簿进行试验。我怀疑我的 displayPerson 函数有问题。不能同时返回多个变量吗?

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'

最佳答案

只是

def myFunc():
return 1, 2

在调用者中

var1, var2 = yourFunc()

关于python - 如何从 Python 函数返回多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19531211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com