Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code
Ron Adam
rrr at ronadam.com
Sat Jul 2 14:26:20 EDT 2005
Ralf W. Grosse-Kunstleve wrote:
> class grouping:
>
> def __init__(self, .x, .y, .z):
> # real code right here
The way this would work seems a bit inconsistent to me. Args normally
create local variables that receive references to the objects passed to
them.
In this case, a function/method is *creating* non local names in a scope
outside it's own name space to receive it's arguments. I don't think
that's a good idea.
A better approach is to have a copy_var_to(dest, *var_list) function
that can do it. You should be able to copy only selected arguments, and
not all of them.
copy_var_to(self,x,z)
Not exactly straight forward to do as it runs into the getting an
objects name problem.
> Emulation using existing syntax::
>
> def __init__(self, x, y, z):
> self.x = x
> del x
> self.y = y
> del y
> self.z = z
> del z
The 'del's aren't needed as the references will be unbound as soon as
__init__ is finished. That's one of the reasons you need to do self.x=x
, the other is to share the objects with other methods.
> Is there a way out with Python as-is?
> -------------------------------------
With argument lists that long it might be better to use a single
dictionary to keep them in.
class manager:
def __init__(self, **args):
defaults = {
'crystal_symmetry':None,
'model_indices':None,
'conformer_indices':None,
'site_symmetry_table':None,
'bond_params_table':None,
'shell_sym_tables':None,
'nonbonded_params':None,
'nonbonded_types':None,
'nonbonded_function':None,
'nonbonded_distance_cutoff':None,
'nonbonded_buffer':1,
'angle_proxies':None,
'dihedral_proxies':None,
'chirality_proxies':None,
'planarity_proxies':None,
'plain_pairs_radius':None }
defaults.update(args)
self.data = defaults
# real code
Regards,
Ron
More information about the Python-list
mailing list