0

I do not think real python, tutorials point, or even admired and helpful Al Sweigert has covered a return statement this outlandish!

The code can be found freely available on the Internet at this URL: https://github.com/odoo/odoo/blob/11.0/odoo/osv/expression.py My question concerns the code at lines 715-737. I am most confused by lines 732-736.

Please see the function where the confusing return statement appears (lines 715-737):

def to_ids(value, comodel):
            """ Normalize a single id or name, or a list of those, into a list of ids
                :param {int,long,basestring,list,tuple} value:
                    if int, long -> return [value]
                    if basestring, convert it into a list of basestrings, then
                    if list of basestring ->
                        perform a name_search on comodel for each name
                        return the list of related ids
            """
            names = []
            if isinstance(value, pycompat.string_types):
                names = [value]
            elif value and isinstance(value, (tuple, list)) and all(isinstance(item, pycompat.string_types) for item in value):
                names = value
            elif isinstance(value, pycompat.integer_types):
                return [value]
            if names:
                return list({
                    rid
                    for name in names
                    for rid, rname in comodel.name_search(name, [], 'ilike', limit=None)
                })
            return list(value)

Given the code above I am very confused by this return statement (lines 732-736):

 return list({
     rid
     for name in names
     for rid, rname in comodel.name_search(name, [], 'ilike', limit=None)
 })

So,

1.) a list is returned.

2.) There are handle-bar brackets around the code meaning the code is a set or a dictionary?

3.) How is there a for loop followed by another for loop on the next line but both for loops are on the same column?

4.) How is the 'name' variable in the first for loop referenced in the second for loop when both loops have the exact same indentation?

5.) Why is 'rid' on a line by itself without any commas? How is this syntactically correct?

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
user3808269
  • 1,321
  • 3
  • 21
  • 40
  • 1
    For 2) can confirm It's a `set`, because with a dict comprehension it would be `key: value` syntax instead – rv.kvetch Nov 19 '21 at 04:47
  • 2
    This return statement is very common tbh. – Ch3steR Nov 19 '21 at 04:48
  • 4
    That is a set comprehension. – juanpa.arrivillaga Nov 19 '21 at 04:48
  • 1
    That's a nested `set` comprehension to guarantee uniqueness, converted to a `list` before `return`ing it. The `for` loops could have been on the same line as well, Python doesn't care about line breaks inside brackets. Indentation doesn't matter because it's a continuation of the previous line. – Selcuk Nov 19 '21 at 04:48
  • 1
    Actually this has nothing to do with the return directly. This is simply a set comprehension that happens to be used as a return, but one could assign it to a variable first and then just use the variable as return. – mozway Nov 19 '21 at 04:51
  • Question was closed as a duplicate but the links to the posts that are apparently duplicates are about list comprehensions not set comprehensions. :/ I feel let down. – user3808269 Nov 19 '21 at 04:56
  • I do not appreciate @Ch3steR at all. – user3808269 Nov 19 '21 at 05:01
  • 1
    @user3870315 I'm sorry if it was rude. But I meant to say it's pretty on [SO], you would pretty much find one-liners in most of the answers in python tag. The return may seem convoluted but in a nutshell, its just `list(set([...]))`. You would find one-liners like this more often than not on [SO]. Again I'm sorry if it came out as rude I didn't mean to be condescending in any way. – Ch3steR Nov 19 '21 at 05:15
  • 1
    1. and 2.) You are creating a set (via a set-comprehension), the contents of which are consumed and stored in a list, which is then returned. – Paul M. Nov 19 '21 at 05:43
  • 1
    3. 4. and 5.) You could have written the whole thing in one line, too. As long as all tokens are separated by whitespace, the Python parser won't complain (newlines are also whitespace). Indentation is irrelevant here. It's a nested for-loop set-comprehension. – Paul M. Nov 19 '21 at 05:45
  • This is not normal at all @Ch3steR. I have been coding Python for years and never seen a statement like this. You are doing the end-users of this website a disservice by not letting people consider the unique aspects of my question. – user3808269 Nov 19 '21 at 05:48
  • I did not even close the question. How did I stop people from not considering the unique aspect of your question? – Ch3steR Nov 19 '21 at 06:03
  • @Ch3steR My apologies then for blaming you for closing the question. Well whomever closed the question is making life harder for people like me to learn. The examples I saw, that are apparently similar, have the same set comprehension but the code looks completely different. This makes picking up new concepts more difficult if we can only see them 1 way. My self-esteem is not high-enough to ask questions on SO. :D They should stop filtering for duplicates. Why would they care if there are duplicate questions? Duplicate questions help reinforce concepts for everyone. They are trying to save $? – user3808269 Nov 19 '21 at 16:27
  • @Selcuk Thank-you for one of the most helpful answers! :) – user3808269 Nov 19 '21 at 18:07

0 Answers0