Hi
You quite rightly noted that
m.a = n.a
m.b = m.b
m.c = m.c
is repetitive. Larger such examples cry out for refactoring.
Such can already be done in Python. How about
for key in 'a', 'b', 'c':
setattr(m, key, getattr(n, key))
If you're doing this a lot, how about a helper function:
transfer_attrs(tgt, src, keys)
Here's the help messages for the builtin commands setattr and getattr.
They're a bit like item access for a dictionary.
setattr(obj, name, value, /)
Sets the named attribute on the given object to the specified value.
setattr(x, 'y', v) is equivalent to ``x.y = v''
getattr(...)
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, 'y') is equivalent to
x.y.
When a default argument is given, it is returned when the attribute
doesn't
exist; without it, an exception is raised in that case.
--
Jonathan
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/2IYOYUBS6HWZKIVOSV6ETWSGYS4EB5RT/
Code of Conduct: http://python.org/psf/codeofconduct/