0

I am working on a solo project and I find myself using very often the following:

list_obj = list(map(func, list_obj))

I want to just add the map functionality to the list class to avoid bloat and make it easier for me, so I created the class:

class list(list):
    def map(self, func):
        return type(self)(map(func, self))

This works fine when creating instances of lists using list(), but the issue is that instantiating a list using brackets [] or list comprehensions returns an instance of list with the old behavior. Is there a way to overload these instantiation methods?

George
  • 135
  • 5
  • 4
    Overriding *list* is a bad idea – DarkKnight Aug 16 '23 at 07:10
  • 2
    Why not just use `[f(i) for i in list_obj]`, it's the "pythonic" way – mousetail Aug 16 '23 at 07:14
  • My functions are typically lambda expressions, which would turn list comprehensions a bit uglier, eg `[(lambda x:g(x))(i) for i in list_obj)]` (in a way that g could not be written `[g(i) for i in list_obj]`) – George Aug 16 '23 at 07:22
  • Also, my intention is to concatenate several of these methods, eg `list_obj.map(f1).filter(f2).map(f3)...`, which would be tedious and unwieldy doing list comprehensions. – George Aug 16 '23 at 07:27
  • Then you could always write: `temp = lambda ......; [temp(i) for i in list_obj]`. Or you could write your own function `listmap` that takes the same arguments as `map` and turns the generator into a `list`. You really don't want to be subclassing list. – Frank Yellin Aug 16 '23 at 07:28
  • I don't think you can change the behavior of `[]` (or comprehensions) in the code. They must be defined at the level of the interpreter. – Ignatius Reilly Aug 16 '23 at 07:48
  • No, not without going to absurd lengths (e.g. pre-processing, ast hacks, etc etc) – juanpa.arrivillaga Aug 16 '23 at 08:06
  • @George no, that doesn't make any sense. You should just do `[g(i) for i in list_obj]`. Every lambda expression that you would pass to map is of the form `map(lambda x: , whatever)` can just be `[ for x in whatever]`. Just use list comprehensions in that case – juanpa.arrivillaga Aug 16 '23 at 08:15
  • @George as for your example of mapping/filtering together, well, the whole point of list comprehensions is to allow for expressive mapping/filtering operations. So indeed, `list_obj.filter(predicate).map(f)` is just `[f(x) for x in list_obj if preciate(x)]`, with your `.map. filter map` example, you can use: `[f3(x) for x in map(f1, list_obj) if f2(x)]` – juanpa.arrivillaga Aug 16 '23 at 08:21
  • if it really gets longer, use intermediate variables. This can remain efficient if you use *generator expressions* which are lazy, and only eagerly evaluating the last step as a list comprehension. Which would be better than what you are doing by doing eager implementations of `map` and `filter`. Also note, if you can live without list-literals, you can always use `MySubclass()` instead of list comprehensions. – juanpa.arrivillaga Aug 16 '23 at 08:23

0 Answers0