【问题标题】:Function call when object is missing from class python类python中缺少对象时的函数调用
【发布时间】:2018-09-12 07:00:03
【问题描述】:

当您尝试对不存在的对象执行类函数时,我正在寻找一种调用函数的方法,类似于字典的__missing__

class Foo:

  def pront(self):
    print(self)

  def doesntexist(self):
    pass #do stuff

object1 = Foo
object1.pront() #works
object2.pront() #calls the doesntexist function

【问题讨论】:

标签: python class oop object missing-data


【解决方案1】:

这是不可能的。 Python 怎么知道 object2 - 不存在,如果它存在,打算Foo

你可以论证它实际上可以是任何类。

【讨论】:

    猜你喜欢
    • 2014-04-22
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    相关资源
    最近更新 更多