如何创建多个类的实例并在不手动操作的情况下传递值?

huangapple go评论101阅读模式
英文:

How can I create multiple instances of a class and pass in values without having to do it manually?

问题

让我们假设我们有一个名为A的类

  1. class A:
  2. def __init__(self, a, b, c):
  3. self.a = a
  4. self.b = b
  5. self.c = c

我想要接受多个输入并将它们作为参数传递给这个类

  1. a = input()
  2. b = input()
  3. c = input()
  4. d = input()
  5. e = input()
  6. f = input()
  7. g = input()
  8. h = input()
  9. i = input()
  10. ins1 = A(a, b, c)
  11. ins2 = A(d, e, f)
  12. ins3 = A(g, h, i)

我如何在不必像这样手动输入的情况下创建这些实例呢?

我尝试使用for循环,但我无法弄清楚如何使它工作。

英文:

Let's say we have a class called A

  1. class A:
  2. def __init__(self, a, b, c):
  3. self.a = a
  4. self.b = b
  5. self.c = c

I want to take multiple inputs and pass them as arguments to the class

  1. a = input()
  2. b = input()
  3. c = input()
  4. d = input()
  5. e = input()
  6. f = input()
  7. g = input()
  8. h = input()
  9. i = input()
  10. ins1 = A(a, b, c)
  11. ins2 = A(d, e, f)
  12. ins3 = A(g, h, i)

How can I create these instances without having to type them like this?

I tried using a for loop but I couldn't figure out how I can make it work.

答案1

得分: 2

如果您不需要变量 a-i 用于除了创建 A 实例之外的其他任何事情,并且可以使用 list(比如 ins)而不是单独的变量 ins1ins2ins3,您可以在循环中创建这些实例:

英文:

If you don't need the variables ai for anything else than creating the instances of A and can use a list (say ins) instead of individual variables ins1, ins2, ins3, you can create the instances in a loop:

  1. ins = [
  2. A(input(), input(), input())
  3. for _ in range(3)
  4. ]

答案2

得分: 0

你有2个常量。一个是你想构建的类的数量,另一个是类构造函数所需的参数数量(输入)。

  1. # 定义常量
  2. NINPUTS = 3
  3. NCLASSES = 3
  4. # 定义类
  5. class A:
  6. def __init__(self, *params: str) -> None:
  7. assert len(params) == NINPUTS
  8. self._a, self._b, self._c = params
  9. def __str__(self) -> str:
  10. return f'a={self._a}, b={self._b}, c={self._c}'
  11. # 获取一定数量输入的便捷函数(作为字符串)
  12. def get_inputs(n: int) -> list[str]:
  13. return [input() for _ in range(n)]
  14. # 使用列表推导构建类的列表
  15. classes = [A(*get_inputs(NINPUTS)) for _ in range(NCLASSES)]
  16. # 打印列表
  17. print(*classes, sep='\n')

这是你的代码,已经翻译好了。

英文:

You have 2 constants. One is the number of classes you want to construct and the other is the number of parameters that the class constructor requires (inputs).

  1. # define the constants
  2. NINPUTS = 3
  3. NCLASSES = 3
  4. # define the class
  5. class A:
  6. def __init__(self, *params: str) -> None:
  7. assert len(params) == NINPUTS
  8. self._a, self._b, self._c = params
  9. def __str__(self) -> str:
  10. return f'a={self._a}, b={self._b}, c={self._c}'
  11. # convenience function for acquiring a number of inputs (as strings)
  12. def get_inputs(n: int) -> list[str]:
  13. return [input() for _ in range(n)]
  14. # list comprehension to construct a list of classes
  15. classes = [A(*get_inputs(NINPUTS)) for _ in range(NCLASSES)]
  16. # print the list
  17. print(*classes, sep='\n')

huangapple
  • 本文由 发表于 2023年6月12日 01:10:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定