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?