Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

empty function object in python

I've heard that python functions are objects, similar to lists or dictionaries, etc. However, what would be a similar way of performing this type of action with a function?

# Assigning empty list to 'a'
a = list()

# Assigning empty function to 'a'
a = lambda: pass
# ???

How would you do this? Further, is it necessary or proper? Here is the sense in which I would like to use it for better context:

I have a QListWidget for selecting items which are associated with keys in a dictionary. The values in this dictionary are also dictionaries, which hold certain properties of the items, which I can add. These certain properties are stored as keys, and the values in them are initialized or updated by calling different functions. So, I'm storing a variable in the window which gets updated when a button is pressed to tell this script which property to update.

As you can see, I would like to store the function to map to the data using the correct function based on the situation.

# Get selection from the list
name = selected_item
# Initialize an empty function
f = lambda: pass
# Use property that is being added now, which was updated by the specific button that was pushed
property_list = items[name][self.property_currently_being_added]
if self.property_currently_being_added == "prop1":
    f = make_property1()
elif self.property_currently_being_added == "prop2":
    f = make_property2()
elif self.property_currently_being_added == "prop3":
    f = make_property3()
elif self.property_currently_being_added == "prop4":
    f = make_property4()

# map the certain function to the data which was retrieved earlier
added_property = map(f, data)
property_list.append(added_property)
like image 298
chase Avatar asked Feb 18 '14 00:02

chase


People also ask

How do you empty an object in Python?

The del keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The del keyword can also be used to delete a list, slice a list, delete a dictionaries, remove key-value pairs from a dictionary, delete variables, etc.

What is object () function in Python?

Python object() Function The object() function returns an empty object. You cannot add new properties or methods to this object. This object is the base for all classes, it holds the built-in properties and methods which are default for all classes.

How do you define an empty function?

An empty function is basically creating a function without defining any operations inside it. A class named Demo contains an empty function named 'my_empty_fun' which is just completed by placing two flower brackets, without adding any functionality into it.

Is there a do nothing function in Python?

In Python, the pass keyword is an entire statement in itself. This statement doesn't do anything: it's discarded during the byte-compile phase.


2 Answers

For completeness and since the title is "empty function object in python", more general case is an empty function object that takes any number of parameters, so you can use it in any callback. It's this one:

callback = lambda *_, **__: None

Explanation is here: http://echochamber.me/viewtopic.php?t=64825

like image 100
ZSvedic Avatar answered Oct 12 '22 10:10

ZSvedic


First, the reason this doesn't work:

a = lamdba: pass

… is that lambda only allows an expression, and defines a function that returns the value of the expression. Since pass is a statement, not an expression, this is illegal.

However, this works just fine:

a = lambda: None

In Python, a function that falls off the end without a return statement always returns None. So, these are equivalent:

def a(): return None
def a(): pass

However, I don't see why you want to write this as a lambda and an assignment anyway; the def is shorter, and more readable, and gives you an introspectable function object with a nice name (a instead of <lambda>), and so on. The only reasons to ever use lambda are when you don't want to give the function a name, or when you need to define the function inside an expression. Obviously neither of those are true, because you use the lambda directly inside an assignment statement. So, just use def.


Meanwhile, this is in a sense an "empty function", or at least as empty as possible (as you can see by, e.g., calling dis.dis(a), it still takes two bytecodes to do nothing but fall off the end and return None), but it's not useful for your case. You don't want an "empty function". If you try passing your a to map, you're just going to get a TypeError, because you're trying to call a function of no arguments with one argument. (Because that's what map does.)

What you might want is an identity function, which just returns its argument as-is. Like this:

def a(x): return x

But I'm not sure that's what you want. Did you want to append data as-is in that case? Or did you want to do something different, like return early, or raise an exception, or not append anything, or …?


Finally, I don't see why you want a function at all. Why not just not call map if you have nothing to map? You have a perfectly good else clause that already catches that case (especially handy if what you want to do is return early or raise…). Or, if you prefer, you can start with f = None, and then use an if f: do decide whether to map or not. Or, if you really want:

added_property = [f(element) if f else element for element in data]

… or …

added_property = map(f, data) if f else data

As one last note, instead of a long if/elif chain that repeats the same thing over and over again, you might want a dict:

propfuncs = {'prop1': make_property1(),
             'prop2': make_property2(),
             'prop3': make_property3(),
             'prop4': make_property4()}

Then, all that cruft turns into these two lines:

f = propfuncs.get(self.property_currently_being_added)
added_property = map(f, data) if f else data

Or course an even better design might be to replace all those make_propertyN functions with a single function that you call as make_property(1) or make_property('prop1')… but without seeing what they actually do, I can't be sure of that.

like image 25
abarnert Avatar answered Oct 12 '22 10:10

abarnert