Cameron Simpson <c...@zip.com.au> writes:

> This passes the local variables inside test1() to "condition" as a
> single parameter. Now, I grant that vars['i'] is a miracle of
> tediousness. So consider this elaboration:
>
>  from collections import namedtuple
>
>  condition_test = lambda vars: vars.i + vars.j > 4
>
>  def test1(a, b, condition):
>    for i, j in zip(a,b):
>      c = i + j
>      vars = locals()
>      varnames = list(vars.keys())
>      varstupletype = namedtuple("locals", varnames)
>      varstuple = varstupletype(*[ vars[k] for k in varnames ])
>      if condition(varstuple):
>        print("Foo")
>
> Here, the condition_test function/lambda uses "vars.i" and "vars.j",
> which i think you'll agree is easier to read and write. The price is
> the construction of a "namedtuple" to hold the variable name
> values. See:
>
>  https://docs.python.org/3/library/collections.html#collections.namedtuple
>

This is probably getting off topic, but is there any relevant difference
or benefit to using namedtuple instead of something like types.SimpleNamespace?

https://docs.python.org/3/library/types.html#additional-utility-classes-and-functions



-- 
A hundred men did the rational thing. The sum of those rational choices was
called panic. Neal Stephenson -- System of the world
http://www.graune.org/GnuPG_pubkey.asc
Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A  5828 5476 7E92 2DB4 3C99
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to