python - 如何调用没有参数的函数?
问题描述
我正在尝试调用该 def listener
函数,但我是 firebase 流的新手,所以我不知道 event
参数是如何工作的,而且我无法在没有参数的情况下调用该函数。无论如何,任何知道我都可以调用该方法的人。我会很感激。
class Notifications(Screen):
notificationslist = ObjectProperty(None)
def listener(self, event):
notifications_screen = self.manager.get_screen('notif')
print(event.event_type) # can be 'put' or 'patch'
print(event.path) # relative to the reference, it seems
print(event.data) # new data at /reference/event.path. None if deleted
notifications = event.data
if notifications.items() == None:
return
else:
for key, value in notifications.items():
thevalue = value
notifications_screen.notificationslist.adapter.data.extend([value[0:17] + '\n' + value[18:]])
print(thevalue)
id = (thevalue[thevalue.index("(") + 1:thevalue.rindex(")")])
print(id)
解决方案
如果您希望在不传递参数的情况下调用函数,则可以在 Python中使用默认参数。
如果在函数调用期间未提供参数,则函数参数采用默认值。
class Notifications(Screen):
notificationslist = ObjectProperty(None)
def listener(self, event = None):
notifications_screen = self.manager.get_screen('notif')
print(event.event_type) # can be 'put' or 'patch'
print(event.path) # relative to the reference, it seems
print(event.data) # new data at /reference/event.path. None if deleted
notifications = event.data
if notifications.items() == None:
return
else:
for key, value in notifications.items():
thevalue = value
notifications_screen.notificationslist.adapter.data.extend([value[0:17] + '\n' + value[18:]])
print(thevalue)
id = (thevalue[thevalue.index("(") + 1:thevalue.rindex(")")])
print(id)
推荐阅读
- r - R rbind 并使用 dplyr 进行分组
- reactjs - 如何在 React 函数组件中使用数组状态?
- javascript - ckeditor 自动删除样式 css
- ruby-on-rails - REST Api 和语义版本控制
- apache-kafka - Kafka Schema 注册表 - 在容器中发布动态端口
- android - 单独的开发和生产崩溃报告
- java - 当我们访问实例变量时,JVM会创建一个新的引用吗?
- ruby - 如何将变量从一种方法访问到ruby中的另一种方法?
- python - 为什么我不能用 range() 打印一个随机整数 numpy 数组
- bash - 我想验证我的设置(manifest.jps)的占位符是否已设置或不在我的 bash 中使用它们(bash 在清单中调用)