Skip to content

Commit a785e6b

Browse files
authored
Rephrase the "Private Attributes and Aliases" section (#1351)
It took me a bit to figure out what was meant by the link to the characteristic issue and why there is no such thing as a private argument. Incorporate (my understanding of) the meaning inline and also provide an example of how to work around the _1 syntax error.
1 parent 0d7e434 commit a785e6b

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

docs/init.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,23 @@ We don't think that your business model and your serialization format should be
6666
## Private Attributes and Aliases
6767

6868
One thing people tend to find confusing is the treatment of private attributes that start with an underscore.
69-
*attrs* follows the doctrine that [there is no such thing as a private argument](https://github.com/hynek/characteristic/issues/6) and strips the underscores from the name when writing the `__init__` method signature:
69+
Although there is [a convention](https://docs.python.org/3/tutorial/classes.html#tut-private) that members of an object that start with an underscore should be treated as private, consider that a core feature of *attrs* is to automatically create an `__init__` method whose arguments correspond to the members.
70+
There is no corresponding convention for private arguments: the entire signature of a function is its public interface to be used by callers.
71+
72+
However, it is sometimes useful to accept a public argument when an object is constructed, but treat that attribute as private after the object is created, perhaps to maintain some invariant.
73+
As a convenience for this use case, the default behavior of *attrs* is that if you specify a member that starts with an underscore, it will strip the underscore from the name when it creates the `__init__` method signature:
7074

7175
```{doctest}
7276
>>> import inspect
7377
>>> from attrs import define
7478
>>> @define
75-
... class C:
76-
... _x: int
77-
>>> inspect.signature(C.__init__)
78-
<Signature (self, x: int) -> None>
79+
... class FileDescriptor:
80+
... _fd: int
81+
>>> inspect.signature(FileDescriptor.__init__)
82+
<Signature (self, fd: int) -> None>
7983
```
8084

81-
There really isn't a right or wrong, it's a matter of taste.
82-
But it's important to be aware of it because it can lead to surprising syntax errors:
85+
Even if you're not using this feature, it's important to be aware of it because it can lead to surprising syntax errors:
8386

8487
```{doctest}
8588
>>> @define
@@ -92,6 +95,7 @@ SyntaxError: invalid syntax
9295

9396
In this case a valid attribute name `_1` got transformed into an invalid argument name `1`.
9497

98+
Whether this feature is useful to you is a matter of taste.
9599
If your taste differs, you can use the *alias* argument to {func}`attrs.field` to explicitly set the argument name.
96100
This can be used to override private attribute handling, or make other arbitrary changes to `__init__` argument names.
97101

@@ -101,8 +105,9 @@ This can be used to override private attribute handling, or make other arbitrary
101105
... class C:
102106
... _x: int = field(alias="_x")
103107
... y: int = field(alias="distasteful_y")
108+
... _1: int = field(alias="underscore1")
104109
>>> inspect.signature(C.__init__)
105-
<Signature (self, _x: int, distasteful_y: int) -> None>
110+
<Signature (self, _x: int, distasteful_y: int, underscore1: int) -> None>
106111
```
107112

108113
(defaults)=

0 commit comments

Comments
 (0)