I have written this helper function. I want to know if there is an even better approach, considering that I do not want to add a dependency to any additional module.

def _isobjectInList(lst, name, criteria='startswith'):
    """ Find item in the list  starting or ending with name passed.

        :param lst: list of items/ objects
        :type lst: list

        :param name: name starting or ending with.
        :param name: string

        :param criteria: function of string obect whether startswith or endswith
        :param criteria: built-in method of str object 
    return [itemfound for itemfound in lst if getattr(itemfound, criteria)(name)]
  • \$\begingroup\$ Your indentation and docstring are incorrect, and this doesn't follow the style guide. Could you show some example usage? \$\endgroup\$ – jonrsharpe May 30 '15 at 14:49
  • \$\begingroup\$ sorry that was issue as I typed the return part instead of copy pasting. \$\endgroup\$ – Ciasto piekarz May 30 '15 at 15:31
  • \$\begingroup\$ I have rolled back the last edit. Please see What to do when someone answers. \$\endgroup\$ – 200_success May 31 '15 at 2:47

There are a few issues with the documentation:

  • The second entry for each parameter should be :type for all three parameters;
  • The criteria parameter (which should really be named criterion) isn't a built-in method, it's the name of a method; and
  • The description of the function only applies if the third parameter is 'startswith' or 'endswith', but that isn't actually a limitation the function has (similarly, see the description of the name parameter).

Also the name isn't quite right (even allowing for PEP-8 violation); given the name, I'd expect a bool back, not a list.

Given how trivial the function is, though, I would probably just inline the list comprehension where needed. It doesn't handle multiple positional arguments or any keyword arguments to the method, but I'm not sure implementing that would be worthwhile.

  • \$\begingroup\$ So you mean its not wroth using list comprehension for how trivial this function is? I was considering the point of performance as I think List Comprehensions are performance intuitive, though I agree with the part that readability matters. Reading your answer makes me feel I have done premature optimisation which I shouldn't have done . \$\endgroup\$ – Ciasto piekarz May 30 '15 at 16:48
  • \$\begingroup\$ @SànLuthra no, I mean there's no point wrapping the list comprehension in a function. lst = _isobjectInList(lst, 'foo', 'endswith') is less readable than just lst = [item for item in lst if item.endswith('foo')]; you don't gain anything by extracting it into a function. \$\endgroup\$ – jonrsharpe May 30 '15 at 16:50
  • \$\begingroup\$ oh, OK, the reason why I wrapped is because I am using this at 3 places and I though it would better to make a function instead and I have also encountered this scenario many times so I though I should make a function for future uses. \$\endgroup\$ – Ciasto piekarz May 30 '15 at 16:53
  • \$\begingroup\$ @SànLuthra but all you've ended up with is a less flexible version of filter! \$\endgroup\$ – jonrsharpe May 30 '15 at 16:57

Use first class functions, not hardcoded strings:

def _isobjectInList(lst, name, criteria=str.startswith):
    >>> _isobjectInList(['foobaz','null'],'foo')
    return [i for i in lst if criteria(i, name)]

also give it a better name

  • \$\begingroup\$ i agree with you about using first class function part but I am not sure the way you have written is more readable then using getattr also I have never seen somebody having default argument like str.startswith convention ! \$\endgroup\$ – Ciasto piekarz May 30 '15 at 15:39
  • \$\begingroup\$ @SànLuthra I kept str.startswith because you used it \$\endgroup\$ – Caridorc May 31 '15 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.