gpt4 book ai didi

python - 在 Python 类中重新分配关键字参数

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

我对如何在启动类时更改定义为关键字参数的属性值感到困惑。我希望能够将它们初始化为某个默认值,然后根据用户输入更改它们,但我不知道该怎么做。

为了说明,我制作了一个根据用户输入制作盒子的类。用户首先选择框的类型,然后选择与该框关联的属性。某些属性,如长度、高度、宽度和颜色,是每个框的属性,无论其类型如何。但是对于某些类型的盒子,我想要求输入有关其他参数的信息。我知道我可以使用类似以下内容打印出关键字值:

for key in kwargs:
print("Here are my keywords and values: %s: %s" % (key, kwargs[key]))

但我不知道如何改变它们的值。这是我做的课。我正在寻找 elif inp = '5': 之后的部分,我在其中放置了 continue 并在注释中写下了我要尝试做的事情。显然,我不能调用变量 extra_parameter1,那么我该如何访问它呢?

class Box():

def __init__(self, **extra_parameters):
self.length = 4
self.height = 3
self.width = 2
self.color = None
self.__dict__.update(extra_parameters)


def get_parameters(self):#, *extra_parameters):
inp = 0
attrs = vars(self)
while inp != '':
for i in range(len(attrs)):
print("{num}. {param}: {value}".format(num=i+1, param=list(attrs.keys())[i], value=list(attrs.values())[i]))
inp = input("Enter your selection: ")
if inp == '1':
self.length = input("Enter the desired length: ")
elif inp == '2':
self.height = input("Enter the desired height: ")
elif inp == '3':
self.width = input("Enter the desired width: ")
elif inp == '4':
self.color = input("Enter the desired color: ")
elif inp == '5':
continue
#self.extra_parameter1 = input("Enter the desired " + extra_parameter1 + ": ")
elif inp == '6':
continue
#self.extra_parameter2 = input("Enter the desired " + extra_parameter2 + ": ")
elif inp == '':
print("Accept parameters")


box_type = input("Which box would you like to make?\n1. Spring\n2. Slip\n3. Prototype \n4. Two-spring\n\n")


if box_type == '1':
spring=Box(spring_strength = 4)
spring.get_parameters()
elif box_type == '2':
slip=Box()
slip.get_parameters()
elif box_type == '3':
proto=Box()
proto.get_parameters()
elif box_type == '4':
two_spring=Box(left_sprint = 8, right_spring = 8)
two_spring.get_parameters()

最佳答案

attrs.keys() 的顺序是未定义的,所以每次修改attrs 的内容时它都会改变。并且不能保证订单将对应于您的硬编码索引。

来自Python 3 docs :

Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions

最好先将键复制到一个列表中,这样您就可以使用固定的(尽管仍然是任意的)顺序。然后您可以通过索引安全地访问所有参数,无需硬编码:

class Box():

def __init__(self, **extra_parameters):
self.length = 4
self.height = 3
self.width = 2
self.color = None
self.__dict__.update(extra_parameters)

def get_parameters(self):
inp = 0
attrs = vars(self)
keys = [k for k in attrs.keys()] # store keys as a list, in order to fix their order

while inp != '':
for i, k in enumerate(keys):
print("{num}. {param}: {value}".format(num=i+1, param=k.replace('_', ' '), value=attrs[k]))
inp = input("\nEnter your selection: ")
if inp == '':
print("Accept parameters")
else:
i = int(inp) - 1
attrs[keys[i]] = input("Enter the desired {param}: ".format(param=keys[i].replace('_', ' ')))


box_type = input("Which box would you like to make?\n1. Spring\n2. Slip\n3. Prototype \n4. Two-spring\n\n")

if box_type == '1':
spring=Box(spring_strength = 4)
spring.get_parameters()
elif box_type == '2':
slip=Box()
slip.get_parameters()
elif box_type == '3':
proto=Box()
proto.get_parameters()
elif box_type == '4':
two_spring=Box(left_spring = 8, right_spring = 8)
two_spring.get_parameters()

关于python - 在 Python 类中重新分配关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51076565/

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