如何动态打印存储在字典值中的函数名?
how to dynamically print function names stored in dict values?
我对 python 和一般编码真的很陌生。这是我正在为我的游戏创意实施各种模块化输入系统的一个小项目。只是为了真正学习。任何帮助将不胜感激,即使它只是关于如何表达我正在寻找的内容?
我希望每个页面(例如:介绍、菜单、统计信息等)都有自己的选项,并分配给一个数字值,该数字值会在您输入所需数字时自动更新。
到目前为止,我无法完全理解我所管理的内容,花了相当长的时间在教程、书籍、帖子和一些应用程序上,试图让它继续下去,但我现在感到被困住了。我尝试了很多不同的方法,这是我最接近我想做的事情的方法。
class Displays():
def __init__(self, name, heading, pageOptions):
self.name = name
self.heading = heading
self.pageOptions = pageOptions
def printPage(self):
print(' *** ' + self.heading + ' *** ')
print(self.name)
print(self.pageOptions) #I would like to replace this with something like the line below
#print(self.pageOptions().__name__)
def pageInput(pageOptions):
pInput = int(input('Num: '))
while pInput in pageOptions:
print(pageOptions[pInput]())
print(pageOptions[pInput].__name__)
return pInput
else:
print('Invalid')
class Intros(Displays):
pass
def Hello():
print('Working hello')
def Bye():
print('Working Bye')
def Exit():
print('Working Exit')
exit()
intro1 = Intros('Start', 'Starting Screen', {1 : Hello, 2 : Bye, 3 : Exit})
intro2 = Intros('Second', 'Second screen', {1 : 'Hey', 2 : 'Boo', 3 : 'Leave'})
intro3 = Intros('Third', 'Third screen', {1 : 'Hi', 2 : 'Good-Bye', 3 : 'Go Away'})
gL = True
while gL:
Displays.printPage(intro1)
Displays.pageInput(intro1.pageOptions)
我目前得到的->
*** Starting Screen ***
Start
{1: <function Hello at 0x000002D771AEC1E0>, 2: <function Bye at 0x000002D774110730>, 3: <function Exit at 0x000002D7741107B8>}
Num: 1
Working hello
None
Hello
*** Starting Screen ***
Start
{1: <function Hello at 0x000002D771AEC1E0>, 2: <function Bye at 0x000002D774110730>, 3: <function Exit at 0x000002D7741107B8>}
Num: 3
Working Exit
>>>
--------------------------------------------------------------------------
*** Starting Screen ***
Start
{1: <function Hello at 0x000002603AC9C1E0>, 2: <function Bye at 0x000002603D2C0730>, 3: <function Exit at 0x000002603D2C07B8>}
Traceback (most recent call last):
File "C:/Users/Zander/AppData/Local/Programs/Python/Python37/accs.py", line 50, in <module>
Displays.printPage(intro1)
File "C:/Users/Zander/AppData/Local/Programs/Python/Python37/accs.py", line 14, in printPage
print(self.pageOptions().__name__)
TypeError: 'dict' object is not callable
>>>
当用户输入 1-3 时,我希望它显示名称,运行 函数并更新它切换到的选项。这样我就可以创建故事情节和一切,就像我在 intro1 中所做的那样。
我想你可以用字典理解来代替那一行。
print({n: f.__name__ for n, f in self.pageOptions.items()})
字典有一个 items
方法,它将 return 一个可迭代对象。该可迭代对象的每次迭代都包含一个 tuple
,如 (<key>, <value>,)
。这可以在字典理解中使用,以构建一个内容略有修改的新 dict
。
一种选择是调整page options结构,包含名称信息,可以更详细的介绍你的功能,灵活,独立于功能名称,如下代码
此外,您不应该使用 Class 调用实例方法,尽管您可以在 python 中调用。
class Displays():
def __init__(self, name, heading, pageOptions):
self.name = name
self.heading = heading
self.pageOptions = pageOptions
def printPage(self):
print(' *** ' + self.heading + ' *** ')
print(self.name)
for key in self.pageOptions:
print(' ', key, self.pageOptions[key]['name'])
def pageInput(self):
pInput = int(input('Num: '))
if pInput in self.pageOptions:
print(self.pageOptions[pInput]['entry']())
else:
print('Invalid')
class Intros(Displays):
pass
def Hello():
print('Working hello')
def Bye():
print('Working Bye')
def Exit():
print('Working Exit')
exit()
intro1 = Intros('Start', 'Starting Screen',
{
1: {'name': 'Hello', 'entry': Hello},
2: {'name': 'Bye', 'entry': Bye},
3: {'name': 'Exit', 'entry': Exit}
})
gL = True
while gL:
intro1.printPage()
intro1.pageInput()
我对 python 和一般编码真的很陌生。这是我正在为我的游戏创意实施各种模块化输入系统的一个小项目。只是为了真正学习。任何帮助将不胜感激,即使它只是关于如何表达我正在寻找的内容?
我希望每个页面(例如:介绍、菜单、统计信息等)都有自己的选项,并分配给一个数字值,该数字值会在您输入所需数字时自动更新。
到目前为止,我无法完全理解我所管理的内容,花了相当长的时间在教程、书籍、帖子和一些应用程序上,试图让它继续下去,但我现在感到被困住了。我尝试了很多不同的方法,这是我最接近我想做的事情的方法。
class Displays():
def __init__(self, name, heading, pageOptions):
self.name = name
self.heading = heading
self.pageOptions = pageOptions
def printPage(self):
print(' *** ' + self.heading + ' *** ')
print(self.name)
print(self.pageOptions) #I would like to replace this with something like the line below
#print(self.pageOptions().__name__)
def pageInput(pageOptions):
pInput = int(input('Num: '))
while pInput in pageOptions:
print(pageOptions[pInput]())
print(pageOptions[pInput].__name__)
return pInput
else:
print('Invalid')
class Intros(Displays):
pass
def Hello():
print('Working hello')
def Bye():
print('Working Bye')
def Exit():
print('Working Exit')
exit()
intro1 = Intros('Start', 'Starting Screen', {1 : Hello, 2 : Bye, 3 : Exit})
intro2 = Intros('Second', 'Second screen', {1 : 'Hey', 2 : 'Boo', 3 : 'Leave'})
intro3 = Intros('Third', 'Third screen', {1 : 'Hi', 2 : 'Good-Bye', 3 : 'Go Away'})
gL = True
while gL:
Displays.printPage(intro1)
Displays.pageInput(intro1.pageOptions)
我目前得到的->
*** Starting Screen ***
Start
{1: <function Hello at 0x000002D771AEC1E0>, 2: <function Bye at 0x000002D774110730>, 3: <function Exit at 0x000002D7741107B8>}
Num: 1
Working hello
None
Hello
*** Starting Screen ***
Start
{1: <function Hello at 0x000002D771AEC1E0>, 2: <function Bye at 0x000002D774110730>, 3: <function Exit at 0x000002D7741107B8>}
Num: 3
Working Exit
>>>
--------------------------------------------------------------------------
*** Starting Screen ***
Start
{1: <function Hello at 0x000002603AC9C1E0>, 2: <function Bye at 0x000002603D2C0730>, 3: <function Exit at 0x000002603D2C07B8>}
Traceback (most recent call last):
File "C:/Users/Zander/AppData/Local/Programs/Python/Python37/accs.py", line 50, in <module>
Displays.printPage(intro1)
File "C:/Users/Zander/AppData/Local/Programs/Python/Python37/accs.py", line 14, in printPage
print(self.pageOptions().__name__)
TypeError: 'dict' object is not callable
>>>
当用户输入 1-3 时,我希望它显示名称,运行 函数并更新它切换到的选项。这样我就可以创建故事情节和一切,就像我在 intro1 中所做的那样。
我想你可以用字典理解来代替那一行。
print({n: f.__name__ for n, f in self.pageOptions.items()})
字典有一个 items
方法,它将 return 一个可迭代对象。该可迭代对象的每次迭代都包含一个 tuple
,如 (<key>, <value>,)
。这可以在字典理解中使用,以构建一个内容略有修改的新 dict
。
一种选择是调整page options结构,包含名称信息,可以更详细的介绍你的功能,灵活,独立于功能名称,如下代码
此外,您不应该使用 Class 调用实例方法,尽管您可以在 python 中调用。
class Displays():
def __init__(self, name, heading, pageOptions):
self.name = name
self.heading = heading
self.pageOptions = pageOptions
def printPage(self):
print(' *** ' + self.heading + ' *** ')
print(self.name)
for key in self.pageOptions:
print(' ', key, self.pageOptions[key]['name'])
def pageInput(self):
pInput = int(input('Num: '))
if pInput in self.pageOptions:
print(self.pageOptions[pInput]['entry']())
else:
print('Invalid')
class Intros(Displays):
pass
def Hello():
print('Working hello')
def Bye():
print('Working Bye')
def Exit():
print('Working Exit')
exit()
intro1 = Intros('Start', 'Starting Screen',
{
1: {'name': 'Hello', 'entry': Hello},
2: {'name': 'Bye', 'entry': Bye},
3: {'name': 'Exit', 'entry': Exit}
})
gL = True
while gL:
intro1.printPage()
intro1.pageInput()