您可以从 class 中的方法创建实例吗?
Can you create an instance from a method within a class?
我正在编写一个程序来创建字符串中字符的组合。我有一个名为 StringRecomb 的 class,它将两个输入作为列表中的字符串,即 ["AA"、"BB"]。重组方法 returns 字符从列表中的字符串左右反转,并且 returns 它们在一个集合中,即 {AB, BA}。
class StringRecomb:
def __init__(self,strings):
self.strings= strings
def recombinations(self):
//some code to create recombinations left and right char of each string in list
return recombinations_set
def method1(self):
//code currently to access the recombinations list:
recombinations_set = self.recombinations()
//do something with recombinations list to get method1_out
return method1_out
我想问的是reaction方法中返回的reactions可以是instance吗?然后在 class 内的其他方法中使用和操作。例如,recombinations2 从第一个重组方法中获取 self.recombinations 并对其进行更改。像...(* 我更改了代码的地方)
class StringRecomb:
def __init__(self,strings):
self.strings= strings
def recombinations(self):
//some code to create recombinations left and right char of each string in list
return self.recombinations_set *
def method1(self, self.recombinations_set): *
//use self.recombinations_set from the parameter of the method to access the set
return method1_out
def recombinations2(self, self.recombinations_set): *
//Method to change the self.recombinations to right to left i.e. {BA, AB} *
return self.recombinations *
我希望我已经解释得够清楚了。
我不太明白你的意思,但你可以像这样将重组存储在 class 中:
class StringRecomb:
def __init__(self, strings):
self.strings = strings
self.recombinations = None
def recombinations(self):
# some code to create recombinations left and right char of each string in list
recombinations_set = foo()
self.recombinations = recombinations_set
def method1(self):
# code currently to access the recombinations list
self.recombinations = foo2()
def recombinations2(self):
# Method to change the self.recombinations to right to left i.e. {BA, AB} *
self.recombinations = foo3()
我正在编写一个程序来创建字符串中字符的组合。我有一个名为 StringRecomb 的 class,它将两个输入作为列表中的字符串,即 ["AA"、"BB"]。重组方法 returns 字符从列表中的字符串左右反转,并且 returns 它们在一个集合中,即 {AB, BA}。
class StringRecomb:
def __init__(self,strings):
self.strings= strings
def recombinations(self):
//some code to create recombinations left and right char of each string in list
return recombinations_set
def method1(self):
//code currently to access the recombinations list:
recombinations_set = self.recombinations()
//do something with recombinations list to get method1_out
return method1_out
我想问的是reaction方法中返回的reactions可以是instance吗?然后在 class 内的其他方法中使用和操作。例如,recombinations2 从第一个重组方法中获取 self.recombinations 并对其进行更改。像...(* 我更改了代码的地方)
class StringRecomb:
def __init__(self,strings):
self.strings= strings
def recombinations(self):
//some code to create recombinations left and right char of each string in list
return self.recombinations_set *
def method1(self, self.recombinations_set): *
//use self.recombinations_set from the parameter of the method to access the set
return method1_out
def recombinations2(self, self.recombinations_set): *
//Method to change the self.recombinations to right to left i.e. {BA, AB} *
return self.recombinations *
我希望我已经解释得够清楚了。
我不太明白你的意思,但你可以像这样将重组存储在 class 中:
class StringRecomb:
def __init__(self, strings):
self.strings = strings
self.recombinations = None
def recombinations(self):
# some code to create recombinations left and right char of each string in list
recombinations_set = foo()
self.recombinations = recombinations_set
def method1(self):
# code currently to access the recombinations list
self.recombinations = foo2()
def recombinations2(self):
# Method to change the self.recombinations to right to left i.e. {BA, AB} *
self.recombinations = foo3()