hynek / characteristic

Please use attrs instead!
https://attrs.readthedocs.io/
MIT License
84 stars 19 forks source link

Allow initialiser arguments to be mapped to alternative (private) attribute names #6

Closed wallrj closed 10 years ago

wallrj commented 10 years ago

In Twisted we try hard to limit the public API in new classes so it would be nice if with_init had some way to map an argument name to an alternative private attribute name.

In fact it might even be enough to have a rule that argument names beginning with _ would be stored as private attributes but supplied without the _ prefix.

twisted.python.util.FancyStrMixin has a horrible system where you can supply a tuple of (attr_name, display_name). I suppose you could do something like that here.

Or add a new Attribute class with an argument_name option...and then allow that to be supplied to the characteristic decorators...if you see what I mean.

hynek commented 10 years ago

Yep, I thought about that before.

I guess I would go two ways:

  1. Each decorator accepts an Attribute class as you suggest.
  2. @attributes additionally adds a convenience argument like private=False that automagically transforms all into _attr if true since that's a rather common case.

Makes sense?

wallrj commented 10 years ago

2 @attributes additionally adds a convenience argument like private=False that automagically transforms all into _attr if true since that's a rather common case.

I think this second option would be convenient, but what I meant in my original comment was that characteristic could implement a policy of always removing leading _ when looking for keyword argument names.

@exarkun is always reminding us that there's no such thing as a private function argument, so maybe he'd approve of that policy.

hynek commented 10 years ago

I think I’ll move this into 14.0 while waiting for glyph sacrificing goats in #9.