-1

I'm looking for a way to call a function when you try to perform a class function on an object that doesn't exist, similar to __missing__ for dictionaries.

class Foo:

  def pront(self):
    print(self)

  def doesntexist(self):
    pass #do stuff

object1 = Foo
object1.pront() #works
object2.pront() #calls the doesntexist function
user236529
  • 115
  • 1
  • 8
  • 1
    Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Kevin Fang Sep 12 '18 at 07:02
  • Relevant: [SO:calling-a-function-of-a-module-by-using-its-name-a-string](https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string?rq=1) – stovfl Sep 12 '18 at 07:27

1 Answers1

1

This is not possible. How would Python know that object2 - which doesn't exist, was intended to be Foo if it had existed?

You could make an argument that it could be literally any class at all.

orlp
  • 112,504
  • 36
  • 218
  • 315