Nick Thorpe
Nick Thorpe

Reputation: 83

What are the drawbacks of changing an internal python method?

I believe I've gotten into a bad habit recently when working with python. Sometimes, an internal method doesn't have all of the features I want, so I redefine the method locally. For example, I wanted to use the filter() method to search for methods in a module.

items=(x if 'query' in x.lower() else False for x in dir(module))

which returns a huge list. I wanted to filter this down with filter(), but the method seemed weirdly limited. The initial solution was:

items_filtered=filter(lambda x: x,items)
print([x for x in items_filtered.__iter__()])

Which seems like more lines than I want. Also I didn't like that iter() wipes the array clean, which is frustrating for notebook environments like Jupiter. I redefined the filter method as:

_filter = filter
class filter(_filter):
  def __init__(self,*args,**kwargs):
    _filter.__init__(*args,**kwargs)
    itr = self.__iter__
    self.values = [x for x in self.__iter__()]
    self.__itr__ = itr

which allows the one liner to be

items=filter(lambda x,(x if 'query' in x.lower() else False for x in dir(module))).values

This is obviously not the 'right' way to handle these situations, but will it break anything when used in the wrong way?

Upvotes: 0

Views: 50

Answers (1)

Paul M.
Paul M.

Reputation: 10809

It seems like you just want a list of all method names (of a given module) whose identifiers contain a specific substring. Why not simply:

print([x for x in dir(module) if "query" in x.lower()])

As far as the filter object goes, a filter is an iterator - it is evaluated lazily, and its contents are consumed as they are yielded/iterated over. Instead of binding a filter object to a variable, just create an anonymous filter object on the line in which you intend to use it:

print([x for x in filter(...)])

or even better:

print(list(filter(...)))

Please don't explicitly use __iter__() to iterate over an iterator...

Upvotes: 2

Related Questions