mirror of
https://github.com/explosion/spaCy.git
synced 2025-07-10 16:22:29 +03:00
Add note on attribute extension defaults (resolves #1587)
This commit is contained in:
parent
954f8cc6d1
commit
c3051e95f7
|
@ -100,7 +100,10 @@ p
|
||||||
| Set a default value for an attribute, which can be overwritten
|
| Set a default value for an attribute, which can be overwritten
|
||||||
| manually at any time. Attribute extensions work like "normal"
|
| manually at any time. Attribute extensions work like "normal"
|
||||||
| variables and are the quickest way to store arbitrary information
|
| variables and are the quickest way to store arbitrary information
|
||||||
| on a #[code Doc], #[code Span] or #[code Token].
|
| on a #[code Doc], #[code Span] or #[code Token]. Attribute defaults
|
||||||
|
| behaves just like argument defaults
|
||||||
|
| #[+a("http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments") in Python functions],
|
||||||
|
| and should not be used for mutable values like dictionaries or lists.
|
||||||
|
|
||||||
+code-wrapper
|
+code-wrapper
|
||||||
+code.
|
+code.
|
||||||
|
|
|
@ -36,6 +36,24 @@ p
|
||||||
if token.text in ('apple', 'orange'):
|
if token.text in ('apple', 'orange'):
|
||||||
token._.set('is_fruit', True)
|
token._.set('is_fruit', True)
|
||||||
|
|
||||||
|
+item
|
||||||
|
| When using #[strong mutable values] like dictionaries or lists as
|
||||||
|
| the #[code default] argument, keep in mind that they behave just like
|
||||||
|
| mutable default arguments
|
||||||
|
| #[+a("http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments") in Python functions].
|
||||||
|
| This can easily cause unintended results, like the same value being
|
||||||
|
| set on #[em all] objects instead of only one particular instance.
|
||||||
|
| In most cases, it's better to use #[strong getters and setters], and
|
||||||
|
| only set the #[code default] for boolean or string values.
|
||||||
|
|
||||||
|
+code-wrapper
|
||||||
|
+code-new.
|
||||||
|
Doc.set_extension('fruits', getter=get_fruits, setter=set_fruits)
|
||||||
|
|
||||||
|
+code-old.
|
||||||
|
Doc.set_extension('fruits', default={})
|
||||||
|
doc._.fruits['apple'] = u'🍎' # all docs now have {'apple': u'🍎'}
|
||||||
|
|
||||||
+item
|
+item
|
||||||
| Always add your custom attributes to the #[strong global] #[code Doc]
|
| Always add your custom attributes to the #[strong global] #[code Doc]
|
||||||
| #[code Token] or #[code Span] objects, not a particular instance of
|
| #[code Token] or #[code Span] objects, not a particular instance of
|
||||||
|
|
Loading…
Reference in New Issue
Block a user