0

Still learning Python, and came to find you can store references to functions in variables.

Doing so seems to open up some nice things in classes, where you can start with a base class and later fill in a handler function that varies by the actual object, keeping things nice and abstract/dynamic.

For example:

class vehicle:
    mpg = None
    seats = None

    navigation = function

Where different instances of vehicle could have different navigation functions to start up... some handling navigation maybe by calling its satnav/GPS (for a nice car), others calling the company's instructions (for like a delivery truck)... others not doing navigation at all. That are filled in when an instance or subclass

And then a program with a collection of vehicles could activate the various GPS functions with a simple call as something like vehicles[index].navigation(arguments). (The vehicles is becoming a tenuous example, but let's say each navigation function spawns a separate process for doing navigation as in my real use case)

But I haven't seen any simple way to initialize a variable like navigation to an empty function, that can be called with arguments and not fail. So that if there is no navigation on a vehicle, it just does nothing and carries on happily. pass doesn't look to take arguments. I could define an empty function and call it:

class vehicle:
    mpg = None
    seats = None
    
    def empty_function(self, **kwargs):
        pass  

    navigation = empty_function

Is there no "Nothing" function to do this in a more compact way like a one-liner? I've probably just missed the proper search terms for this, or trying to go about things entirely the wrong way?

JeopardyTempest
  • 136
  • 1
  • 2
  • 10
  • Does this help? https://stackoverflow.com/questions/22738412/a-suitable-do-nothing-lambda-expression-in-python – qrsngky Aug 23 '22 at 10:11
  • If you plan to have several kinds of vehicle where each would have different navigation function/method, then I suggest you learn class inheritance and overriding methods. Would probably save you from a lot of coding. – Jobo Fernandez Aug 23 '22 at 10:16

0 Answers0