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

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

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

问题

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

class A:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

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

a = input()
b = input()
c = input()
d = input()
e = input()
f = input()
g = input()
h = input()
i = input()

ins1 = A(a, b, c)
ins2 = A(d, e, f)
ins3 = A(g, h, i)

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

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

英文:

Let's say we have a class called A

class A:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

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

a = input()
b = input()
c = input()
d = input()
e = input()
f = input()
g = input()
h = input()
i = input()

ins1 = A(a, b, c)
ins2 = A(d, e, f)
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:

ins = [
    A(input(), input(), input())
    for _ in range(3)
]

答案2

得分: 0

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

# 定义常量
NINPUTS = 3
NCLASSES = 3

# 定义类
class A:
    def __init__(self, *params: str) -> None:
        assert len(params) == NINPUTS
        self._a, self._b, self._c = params
    def __str__(self) -> str:
        return f'a={self._a}, b={self._b}, c={self._c}'

# 获取一定数量输入的便捷函数(作为字符串)
def get_inputs(n: int) -> list[str]:
    return [input() for _ in range(n)]

# 使用列表推导构建类的列表
classes = [A(*get_inputs(NINPUTS)) for _ in range(NCLASSES)]

# 打印列表
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).

# define the constants
NINPUTS = 3
NCLASSES = 3

# define the class
class A:
    def __init__(self, *params: str) -> None:
        assert len(params) == NINPUTS
        self._a, self._b, self._c = params
    def __str__(self) -> str:
        return f'a={self._a}, b={self._b}, c={self._c}'

# convenience function for acquiring a number of inputs (as strings)
def get_inputs(n: int) -> list[str]:
    return [input() for _ in range(n)]

# list comprehension to construct a list of classes
classes = [A(*get_inputs(NINPUTS)) for _ in range(NCLASSES)]

# print the list
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:

确定