有一个叫做start_party的函数,它不属于任何类,它是一个独立的函数,可以打印音乐,告诉参与者参加者做出一些有趣的事情,例如跳舞或唱歌。
一个人可以像参加聚会的人一样适合自己,它有两种状态的感觉:连接和断开。可以告诉一个人根据外部功能执行一个动作(方法)。因此,当人们执行动作时,它始于感觉到连接,并以要求实现的任何方法参与外部功能。外部功能停止(开始聚会结束...),因此该人员感到与这一不可思议的时刻断开了联系,因为它通过打印使我们知道。
所有这些经验的实现如下:
class Feeling():
def __init__(self):
self.data_in = 'into connection'
self.data_out = 'out of connection'
class Person():
def __init__(self):
self.feeling = Feeling()
def execute(self, outer_function, inner_function):
print(self.feeling.data_in)
outer_function(self, inner_function)
print(self.feeling.data_out)
def dance(self):
print(' └[∵┌]└[ ∵ ]┘[┐∵]┘ ')
def sing(self):
print('( ◜◒◝ )')
def start_party(party_participant, inner_function):
print('♬♩♪♩')
party_participant.inner_function()
print('♬♩♪♩')
liz = Person()
liz.execute(start_party, dance)
我尽力而为,没有编译,它给了我一个:
NameError:未定义名称“舞蹈”
但是仍然主要的问题不是编译,而是设计。(尽管我也需要修复编译。)
所需的输出应为:
into connection
♬♩♪♩
└[∵┌]└[ ∵ ]┘[┐∵]┘
♬♩♪♩
out of connection
使用getattr()
以获取类的方法来执行,如:
def start_party(party_participant, inner_function):
print('♬♩♪♩')
getattr(party_participant, inner_function)()
print('♬♩♪♩')
class Feeling():
def __init__(self):
self.data_in = 'into connection'
self.data_out = 'out of connection'
class Person():
def __init__(self):
self.feeling = Feeling()
def execute(self, outer_function, inner_function):
print(self.feeling.data_in)
outer_function(self, inner_function)
print(self.feeling.data_out)
def dance(self):
print(' └[∵┌]└[ ∵ ]┘[┐∵]┘ ')
def sing(self):
print('( ◜◒◝ )')
def start_party(party_participant, inner_function):
print('♬♩♪♩')
getattr(party_participant, inner_function)()
print('♬♩♪♩')
liz = Person()
liz.execute(start_party, 'dance')
into connection
♬♩♪♩
└[∵┌]└[ ∵ ]┘[┐∵]┘
♬♩♪♩
out of connection
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句