Skip to content

Commit

Permalink
Change attr.a to attr.ib
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
hynek committed Jan 27, 2015
1 parent c7381fd commit 5ce5e48
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ attrs: Python attributes without boilerplate.
>>> import attr
>>> @attr.s
... class C(object):
... x = attr.a(default_value=42)
... y = attr.a(default_factory=list)
... x = attr.ib(default_value=42)
... y = attr.ib(default_factory=list)
>>> i = C(x=1, y=2)
>>> i
<C(x=1, y=2)>
Expand Down
4 changes: 2 additions & 2 deletions attr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
to_dict,
)
from ._make import (
_make_attr as a,
_make_attr as ib,
s,
)

Expand All @@ -18,5 +18,5 @@


__all__ = [
"a", "s", "ls", "to_dict",
"ib", "s", "ls", "to_dict",
]
25 changes: 10 additions & 15 deletions docs/why.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,15 @@ The difference between :func:`collections.namedtuple`\ s and classes decorated b

.. doctest::

>>> from characteristic import Attribute, attributes
>>> @attributes([Attribute("a", instance_of=int)])
>>> import attr
>>> @attr.s
... class C1(object):
... def __init__(self):
... if self.a >= 5:
... raise ValueError("'a' must be smaller 5!")
... a = attr.ib()
... def print_a(self):
... print self.a
>>> @attributes([Attribute("a", instance_of=int)])
... print self.a
>>> @attr.s
... class C2(object):
... pass
... a = attr.ib()
>>> c1 = C1(a=1)
>>> c2 = C2(a=1)
>>> c1.a == c2.a
Expand All @@ -78,10 +76,6 @@ The difference between :func:`collections.namedtuple`\ s and classes decorated b
False
>>> c1.print_a()
1
>>> C1(a=5)
Traceback (most recent call last):
...
ValueError: 'a' must be smaller 5!


…while namedtuple’s purpose is *explicitly* to behave like tuples:
Expand Down Expand Up @@ -115,10 +109,11 @@ To bring it into perspective, the equivalent of

.. doctest::

>>> @attributes(["a", "b"])
>>> @attr.s
... class SmartClass(object):
... pass
>>> SmartClass(a=1, b=2)
... a = attr.ib()
... b = attr.ib()
>>> SmartClass(1, 2)
<SmartClass(a=1, b=2)>

is
Expand Down
8 changes: 4 additions & 4 deletions tests/test_dark_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

@attr.s
class C1(object):
x = attr.a()
y = attr.a()
x = attr.ib()
y = attr.ib()


foo = None


@attr.s()
class C2(object):
x = attr.a(default_value=foo)
y = attr.a(default_factory=list)
x = attr.ib(default_value=foo)
y = attr.ib(default_factory=list)


class TestDarkMagic(object):
Expand Down

0 comments on commit 5ce5e48

Please sign in to comment.