On 2013-12-03 15:47, Piotr Dobrogost wrote:
> > The getattr function is meant for when your attribute name is in a
> > variable. Being able to use strings that aren't valid identifiers
> > is a side effect.   
> 
> Why do you say it's a side effect?

I think random832 is saying that the designed purpose of setattr()
was to dynamically set attributes by name, so they could later be
accessed the traditional way; not designed from the ground-up to
support non-identifier names.  But because of the getattr/setattr
machinery (dict key/value pairs), it doesn't prevent you from having
non-identifiers as names as long as you use only the getattr/setattr
method of accessing them.

I see non-traditional-identifiers most frequently in test code where
the globals() dictionary gets injected with various objects for
testing purposes, driven by a table with descriptors.  Something like
(untested)

  tests = [
     dict(desc="Test 1", input=10, expected=42),
     dict(desc="Test 2", input=314, expected=159),
     ]
  for test in tests:
    test_name = "test_" + test["desc"]
    globals()[test_name] = generate_test_function(
       test["input"], test["output"])

-tkc



-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to