我是编程的初学者,我需要一些帮助。是否还有其他选项来编写 get_the_thing 函数?在我看来,没有必要使用这么多。
class SomeClass():
def __init__(self, things):
self._1 = things[0]
self._2 = things[1]
self._3 = things[2]
self._4 = things[3]
def get_the_thing(self, bok):
self.bok = int(bok)
if bok == 1:
return (self._1)
if bok == 2:
return (self._2)
if bok == 3:
return (self._3)
if bok == 4:
return (self._4)
答案 0 :(得分:1)
只需存放原样。
class SomeClass():
def __init__(self, things):
self._things = things
def get_the_thing(self, bok):
return self._things[bok - 1]