我可以根据用户需要更改子类对象中的变量吗?如果是,如何,或者有不同的方法?你知道吗
我想要一个变量的子类列表根据用户的需求增长,而不必在init中预先编写*self.*变量,并允许用户创建一个列表。你知道吗
如何处理子类中长度不断变化的列表?你知道吗
class MyClass(object):
def __init__(self, list1, list2): # list 1: keys; list 2: values
len1 = len(list1) # Changing range based on desired keys
for i in range(len1):
self.list1[i] = list2[i] # desired effect
List1 = ['first','second','third']; List2 = [1,2,3];
print(MyClass(List1,List2))
List1.append('fourth'); List2.append(4)
print(MyClass(List1,List2))
所需的输出是[[getattr(item, x) for x in list1] for item in list2]
,第一个实例返回3个值,第二个实例返回4个值。你知道吗
在这个类的实例上尝试getattr(item, x)
时收到的错误是AttributeError: 'MyClass' object has no attribute <key>
,其中<key>
是list1,而不是list1中的值。你知道吗
我的工作是预先填充字段,如:
import re
kLs = ("the", "list", "of", "new", "caught", "values")
let = ['a','b','c','d','e','f']
reAN = re.compile("\w+")
for k in kLs: print('self.'+reAN.search(re.sub(r"\s+", "", k)).group()+' = '+let[kLs.index(k)])
每次我想减一个变量或加一个变量时,我都要这样做。这行不通。有人吗?你知道吗
Same as always:
相关问题 更多 >
编程相关推荐