/usr/share/doc/python-attr/html/examples.html is in python-attr-doc 17.4.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>attrs by Example — attrs 17.4.0 documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '17.4.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="API Reference" href="api.html" />
<link rel="prev" title="Why not…" href="why.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="attrs-by-example">
<span id="examples"></span><h1><code class="docutils literal"><span class="pre">attrs</span></code> by Example<a class="headerlink" href="#attrs-by-example" title="Permalink to this headline">¶</a></h1>
<div class="section" id="basics">
<h2>Basics<a class="headerlink" href="#basics" title="Permalink to this headline">¶</a></h2>
<p>The simplest possible usage is:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">attr</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">Empty</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">>>> </span><span class="n">Empty</span><span class="p">()</span>
<span class="go">Empty()</span>
<span class="gp">>>> </span><span class="n">Empty</span><span class="p">()</span> <span class="o">==</span> <span class="n">Empty</span><span class="p">()</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">Empty</span><span class="p">()</span> <span class="ow">is</span> <span class="n">Empty</span><span class="p">()</span>
<span class="go">False</span>
</pre></div>
</div>
<p>So in other words: <code class="docutils literal"><span class="pre">attrs</span></code> is useful even without actual attributes!</p>
<p>But you’ll usually want some data on your classes, so let’s add some:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">Coordinates</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
</pre></div>
</div>
<p>By default, all features are added, so you immediately have a fully functional data class with a nice <code class="docutils literal"><span class="pre">repr</span></code> string and comparison methods.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">c1</span> <span class="o">=</span> <span class="n">Coordinates</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">c1</span>
<span class="go">Coordinates(x=1, y=2)</span>
<span class="gp">>>> </span><span class="n">c2</span> <span class="o">=</span> <span class="n">Coordinates</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">c2</span>
<span class="go">Coordinates(x=2, y=1)</span>
<span class="gp">>>> </span><span class="n">c1</span> <span class="o">==</span> <span class="n">c2</span>
<span class="go">False</span>
</pre></div>
</div>
<p>As shown, the generated <code class="docutils literal"><span class="pre">__init__</span></code> method allows for both positional and keyword arguments.</p>
<p>If playful naming turns you off, <code class="docutils literal"><span class="pre">attrs</span></code> comes with serious business aliases:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">attr</span> <span class="k">import</span> <span class="n">attrs</span><span class="p">,</span> <span class="n">attrib</span>
<span class="gp">>>> </span><span class="nd">@attrs</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">SeriousCoordinates</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attrib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attrib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">SeriousCoordinates</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">SeriousCoordinates(x=1, y=2)</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">Coordinates</span><span class="p">)</span> <span class="o">==</span> <span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">SeriousCoordinates</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>For private attributes, <code class="docutils literal"><span class="pre">attrs</span></code> will strip the leading underscores for keyword arguments:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">_x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">C(_x=1)</span>
</pre></div>
</div>
<p>If you want to initialize your private attributes yourself, you can do that too:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">_x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">()</span>
<span class="go">C(_x=42)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">__init__() takes exactly 1 argument (2 given)</span>
</pre></div>
</div>
<p>An additional way of defining attributes is supported too.
This is useful in times when you want to enhance classes that are not yours (nice <code class="docutils literal"><span class="pre">__repr__</span></code> for Django models anyone?):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">SomethingFromSomeoneElse</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span>
<span class="gp">>>> </span><span class="n">SomethingFromSomeoneElse</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span>
<span class="gp">... </span> <span class="n">these</span><span class="o">=</span><span class="p">{</span>
<span class="gp">... </span> <span class="s2">"x"</span><span class="p">:</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="p">},</span> <span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)(</span><span class="n">SomethingFromSomeoneElse</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">SomethingFromSomeoneElse</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">SomethingFromSomeoneElse(x=1)</span>
</pre></div>
</div>
<p><a class="reference external" href="https://www.youtube.com/watch?v=3MNVP9-hglc">Subclassing is bad for you</a>, but <code class="docutils literal"><span class="pre">attrs</span></code> will still do what you’d hope for:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">a</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">get_a</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">b</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">c</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">i</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">i</span>
<span class="go">C(a=1, b=2, c=3)</span>
<span class="gp">>>> </span><span class="n">i</span> <span class="o">==</span> <span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">get_a</span><span class="p">()</span>
<span class="go">1</span>
</pre></div>
</div>
<p>The order of the attributes is defined by the <a class="reference external" href="https://www.python.org/download/releases/2.3/mro/">MRO</a>.</p>
<p>In Python 3, classes defined within other classes are <a class="reference external" href="https://www.python.org/dev/peps/pep-3155/">detected</a> and reflected in the <code class="docutils literal"><span class="pre">__repr__</span></code>.
In Python 2 though, it’s impossible.
Therefore <code class="docutils literal"><span class="pre">@attr.s</span></code> comes with the <code class="docutils literal"><span class="pre">repr_ns</span></code> option to set it manually:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">repr_ns</span><span class="o">=</span><span class="s2">"C"</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">class</span> <span class="nc">D</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">>>> </span><span class="n">C</span><span class="o">.</span><span class="n">D</span><span class="p">()</span>
<span class="go">C.D()</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">repr_ns</span></code> works on both Python 2 and 3.
On Python 3 it overrides the implicit detection.</p>
</div>
<div class="section" id="converting-to-collections-types">
<span id="asdict"></span><h2>Converting to Collections Types<a class="headerlink" href="#converting-to-collections-types" title="Permalink to this headline">¶</a></h2>
<p>When you have a class with data, it often is very convenient to transform that class into a <a class="reference external" href="/usr/share/doc/python3-doc/html/library/stdtypes.html#dict" title="(in Python v3.6)"><code class="xref py py-class docutils literal"><span class="pre">dict</span></code></a> (for example if you want to serialize it to JSON):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">asdict</span><span class="p">(</span><span class="n">Coordinates</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">))</span>
<span class="go">{'x': 1, 'y': 2}</span>
</pre></div>
</div>
<p>Some fields cannot or should not be transformed.
For that, <a class="reference internal" href="api.html#attr.asdict" title="attr.asdict"><code class="xref py py-func docutils literal"><span class="pre">attr.asdict()</span></code></a> offers a callback that decides whether an attribute should be included:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">UserList</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">users</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">email</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">password</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">asdict</span><span class="p">(</span><span class="n">UserList</span><span class="p">([</span><span class="n">User</span><span class="p">(</span><span class="s2">"jane@doe.invalid"</span><span class="p">,</span> <span class="s2">"s33kred"</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">User</span><span class="p">(</span><span class="s2">"joe@doe.invalid"</span><span class="p">,</span> <span class="s2">"p4ssw0rd"</span><span class="p">)]),</span>
<span class="gp">... </span> <span class="nb">filter</span><span class="o">=</span><span class="k">lambda</span> <span class="n">attr</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="n">attr</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="s2">"password"</span><span class="p">)</span>
<span class="go">{'users': [{'email': 'jane@doe.invalid'}, {'email': 'joe@doe.invalid'}]}</span>
</pre></div>
</div>
<p>For the common case where you want to <a class="reference internal" href="api.html#attr.filters.include" title="attr.filters.include"><code class="xref py py-func docutils literal"><span class="pre">include</span></code></a> or <a class="reference internal" href="api.html#attr.filters.exclude" title="attr.filters.exclude"><code class="xref py py-func docutils literal"><span class="pre">exclude</span></code></a> certain types or attributes, <code class="docutils literal"><span class="pre">attrs</span></code> ships with a few helpers:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">login</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">password</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="nb">id</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">asdict</span><span class="p">(</span>
<span class="gp">... </span> <span class="n">User</span><span class="p">(</span><span class="s2">"jane"</span><span class="p">,</span> <span class="s2">"s33kred"</span><span class="p">,</span> <span class="mi">42</span><span class="p">),</span>
<span class="gp">... </span> <span class="nb">filter</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">filters</span><span class="o">.</span><span class="n">exclude</span><span class="p">(</span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">User</span><span class="p">)</span><span class="o">.</span><span class="n">password</span><span class="p">,</span> <span class="nb">int</span><span class="p">))</span>
<span class="go">{'login': 'jane'}</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">z</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">asdict</span><span class="p">(</span><span class="n">C</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"2"</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span>
<span class="gp">... </span> <span class="nb">filter</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">filters</span><span class="o">.</span><span class="n">include</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="o">.</span><span class="n">x</span><span class="p">))</span>
<span class="go">{'x': 'foo', 'z': 3}</span>
</pre></div>
</div>
<p>Other times, all you want is a tuple and <code class="docutils literal"><span class="pre">attrs</span></code> won’t let you down:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sqlite3</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">attr</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">a</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">b</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">foo</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">with</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">":memory:"</span><span class="p">)</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">c</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">"CREATE TABLE foo (x INTEGER PRIMARY KEY ASC, y)"</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">"INSERT INTO foo VALUES (?, ?)"</span><span class="p">,</span> <span class="n">attr</span><span class="o">.</span><span class="n">astuple</span><span class="p">(</span><span class="n">foo</span><span class="p">))</span>
<span class="gp">... </span> <span class="n">foo2</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">(</span><span class="o">*</span><span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">"SELECT x, y FROM foo"</span><span class="p">)</span><span class="o">.</span><span class="n">fetchone</span><span class="p">())</span>
<span class="go"><sqlite3.Cursor object at ...></span>
<span class="go"><sqlite3.Cursor object at ...></span>
<span class="gp">>>> </span><span class="n">foo</span> <span class="o">==</span> <span class="n">foo2</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="defaults">
<h2>Defaults<a class="headerlink" href="#defaults" title="Permalink to this headline">¶</a></h2>
<p>Sometimes you want to have default values for your initializer.
And sometimes you even want mutable objects as default values (ever used accidentally <code class="docutils literal"><span class="pre">def</span> <span class="pre">f(arg=[])</span></code>?).
<code class="docutils literal"><span class="pre">attrs</span></code> has you covered in both cases:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">collections</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">Connection</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">socket</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="nd">@classmethod</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">db_string</span><span class="p">):</span>
<span class="gp">... </span> <span class="c1"># ... connect somehow to db_string ...</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">socket</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">ConnectionPool</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">db_string</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">pool</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">Factory</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">))</span>
<span class="gp">... </span> <span class="n">debug</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">get_connection</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pool</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="gp">... </span> <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">debug</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"New connection!"</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">Connection</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db_string</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">free_connection</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">debug</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Connection returned!"</span><span class="p">)</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">pool</span><span class="o">.</span><span class="n">appendleft</span><span class="p">(</span><span class="n">conn</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">cp</span> <span class="o">=</span> <span class="n">ConnectionPool</span><span class="p">(</span><span class="s2">"postgres://localhost"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">cp</span>
<span class="go">ConnectionPool(db_string='postgres://localhost', pool=deque([]), debug=False)</span>
<span class="gp">>>> </span><span class="n">conn</span> <span class="o">=</span> <span class="n">cp</span><span class="o">.</span><span class="n">get_connection</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">conn</span>
<span class="go">Connection(socket=42)</span>
<span class="gp">>>> </span><span class="n">cp</span><span class="o">.</span><span class="n">free_connection</span><span class="p">(</span><span class="n">conn</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">cp</span>
<span class="go">ConnectionPool(db_string='postgres://localhost', pool=deque([Connection(socket=42)]), debug=False)</span>
</pre></div>
</div>
<p>More information on why class methods for constructing objects are awesome can be found in this insightful <a class="reference external" href="http://as.ynchrono.us/2014/12/asynchronous-object-initialization.html">blog post</a>.</p>
<p>Default factories can also be set using a decorator.
The method receives the partially initialized instance which enables you to base a default value on other attributes:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="nd">@y</span><span class="o">.</span><span class="n">default</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">name_does_not_matter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">()</span>
<span class="go">C(x=1, y=2)</span>
</pre></div>
</div>
</div>
<div class="section" id="validators">
<span id="examples-validators"></span><h2>Validators<a class="headerlink" href="#validators" title="Permalink to this headline">¶</a></h2>
<p>Although your initializers should do as little as possible (ideally: just initialize your instance according to the arguments!), it can come in handy to do some kind of validation on the arguments.</p>
<p><code class="docutils literal"><span class="pre">attrs</span></code> offers two ways to define validators for each attribute and it’s up to you to choose which one suites better your style and project.</p>
<div class="section" id="decorator">
<h3>Decorator<a class="headerlink" href="#decorator" title="Permalink to this headline">¶</a></h3>
<p>The more straightforward way is by using the attribute’s <code class="docutils literal"><span class="pre">validator</span></code> method as a decorator.
The method has to accept three arguments:</p>
<ol class="arabic simple">
<li>the <em>instance</em> that’s being validated (aka <code class="docutils literal"><span class="pre">self</span></code>),</li>
<li>the <em>attribute</em> that it’s validating, and finally</li>
<li>the <em>value</em> that is passed for it.</li>
</ol>
<p>If the value does not pass the validator’s standards, it just raises an appropriate exception.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="nd">@x</span><span class="o">.</span><span class="n">validator</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">if</span> <span class="n">value</span> <span class="o">></span> <span class="mi">42</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"x must be smaller or equal to 42"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">C(x=42)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="mi">43</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">x must be smaller or equal to 42</span>
</pre></div>
</div>
</div>
<div class="section" id="callables">
<h3>Callables<a class="headerlink" href="#callables" title="Permalink to this headline">¶</a></h3>
<p>If you want to re-use your validators, you should have a look at the <code class="docutils literal"><span class="pre">validator</span></code> argument to <a class="reference internal" href="api.html#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a>.</p>
<p>It takes either a callable or a list of callables (usually functions) and treats them as validators that receive the same arguments as with the decorator approach.</p>
<p>Since the validators runs <em>after</em> the instance is initialized, you can refer to other attributes while validating:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">x_smaller_than_y</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">if</span> <span class="n">value</span> <span class="o">>=</span> <span class="n">instance</span><span class="o">.</span><span class="n">y</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"'x' has to be smaller than 'y'!"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="p">[</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">instance_of</span><span class="p">(</span><span class="nb">int</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">x_smaller_than_y</span><span class="p">])</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="go">C(x=3, y=4)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">'x' has to be smaller than 'y'!</span>
</pre></div>
</div>
<p>This example also shows of some syntactic sugar for using the <a class="reference internal" href="api.html#attr.validators.and_" title="attr.validators.and_"><code class="xref py py-func docutils literal"><span class="pre">attr.validators.and_()</span></code></a> validator: if you pass a list, all validators have to pass.</p>
<p><code class="docutils literal"><span class="pre">attrs</span></code> won’t intercept your changes to those attributes but you can always call <a class="reference internal" href="api.html#attr.validate" title="attr.validate"><code class="xref py py-func docutils literal"><span class="pre">attr.validate()</span></code></a> on any instance to verify that it’s still valid:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">i</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">5</span> <span class="c1"># works, no magic here</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">'x' has to be smaller than 'y'!</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">attrs</span></code> ships with a bunch of validators, make sure to <a class="reference internal" href="api.html#api-validators"><span class="std std-ref">check them out</span></a> before writing your own:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">instance_of</span><span class="p">(</span><span class="nb">int</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">C(x=42)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="s2">"42"</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">("'x' must be <type 'int'> (got '42' that is a <type 'str'>).", Attribute(name='x', default=NOTHING, factory=NOTHING, validator=<instance_of validator for type <type 'int'>>, type=None), <type 'int'>, '42')</span>
</pre></div>
</div>
<p>Of course you can mix and match the two approaches at your convenience:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">validators</span><span class="o">.</span><span class="n">instance_of</span><span class="p">(</span><span class="nb">int</span><span class="p">))</span>
<span class="gp">... </span> <span class="nd">@x</span><span class="o">.</span><span class="n">validator</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">fits_byte</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">if</span> <span class="ow">not</span> <span class="mi">0</span> <span class="o"><</span> <span class="n">value</span> <span class="o"><</span> <span class="mi">256</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"value out of bounds"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="mi">128</span><span class="p">)</span>
<span class="go">C(x=128)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="s2">"128"</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">("'x' must be <class 'int'> (got '128' that is a <class 'str'>).", Attribute(name='x', default=NOTHING, validator=[<instance_of validator for type <class 'int'>>, <function fits_byte at 0x10fd7a0d0>], repr=True, cmp=True, hash=True, init=True, metadata=mappingproxy({}), type=None, converter=one), <class 'int'>, '128')</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="mi">256</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">value out of bounds</span>
</pre></div>
</div>
<p>And finally you can disable validators globally:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">set_run_validators</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="s2">"128"</span><span class="p">)</span>
<span class="go">C(x='128')</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">set_run_validators</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="s2">"128"</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">("'x' must be <class 'int'> (got '128' that is a <class 'str'>).", Attribute(name='x', default=NOTHING, validator=[<instance_of validator for type <class 'int'>>, <function fits_byte at 0x10fd7a0d0>], repr=True, cmp=True, hash=True, init=True, metadata=mappingproxy({}), type=None, converter=None), <class 'int'>, '128')</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="conversion">
<h2>Conversion<a class="headerlink" href="#conversion" title="Permalink to this headline">¶</a></h2>
<p>Attributes can have a <code class="docutils literal"><span class="pre">converter</span></code> function specified, which will be called with the attribute’s passed-in value to get a new value to use.
This can be useful for doing type-conversions on values that you don’t want to force your callers to do.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">converter</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">o</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="s2">"1"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">o</span><span class="o">.</span><span class="n">x</span>
<span class="go">1</span>
</pre></div>
</div>
<p>Converters are run <em>before</em> validators, so you can use validators to check the final form of the value.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">validate_x</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">attribute</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">if</span> <span class="n">value</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"x must be be at least 0."</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">converter</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">validator</span><span class="o">=</span><span class="n">validate_x</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">o</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="s2">"0"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">o</span><span class="o">.</span><span class="n">x</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="s2">"-1"</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="o">...</span>
<span class="gr">ValueError</span>: <span class="n">x must be be at least 0.</span>
</pre></div>
</div>
</div>
<div class="section" id="metadata">
<span id="id1"></span><h2>Metadata<a class="headerlink" href="#metadata" title="Permalink to this headline">¶</a></h2>
<p>All <code class="docutils literal"><span class="pre">attrs</span></code> attributes may include arbitrary metadata in the form of a read-only dictionary.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">metadata</span><span class="o">=</span><span class="p">{</span><span class="s1">'my_metadata'</span><span class="p">:</span> <span class="mi">1</span><span class="p">})</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">metadata</span>
<span class="go">mappingproxy({'my_metadata': 1})</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">'my_metadata'</span><span class="p">]</span>
<span class="go">1</span>
</pre></div>
</div>
<p>Metadata is not used by <code class="docutils literal"><span class="pre">attrs</span></code>, and is meant to enable rich functionality in third-party libraries.
The metadata dictionary follows the normal dictionary rules: keys need to be hashable, and both keys and values are recommended to be immutable.</p>
<p>If you’re the author of a third-party library with <code class="docutils literal"><span class="pre">attrs</span></code> integration, please see <a class="reference internal" href="extending.html#extending-metadata"><span class="std std-ref">Extending Metadata</span></a>.</p>
</div>
<div class="section" id="types">
<h2>Types<a class="headerlink" href="#types" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal"><span class="pre">attrs</span></code> also allows you to associate a type with an attribute using either the <em>type</em> argument to <a class="reference internal" href="api.html#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a> or – as of Python 3.6 – using <a class="reference external" href="https://www.python.org/dev/peps/pep-0526/">PEP 526</a>-annotations:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">y</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">type</span>
<span class="go"><class 'int'></span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C</span><span class="p">)</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="n">type</span>
<span class="go"><class 'int'></span>
</pre></div>
</div>
<p>If you don’t mind annotating <em>all</em> attributes, you can even drop the <a class="reference internal" href="api.html#attr.ib" title="attr.ib"><code class="xref py py-func docutils literal"><span class="pre">attr.ib()</span></code></a> and assign default values instead:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">typing</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">auto_attribs</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">AutoC</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">cls_var</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">ClassVar</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="mi">5</span> <span class="c1"># this one is ignored</span>
<span class="gp">... </span> <span class="n">l</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">Factory</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">x</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">... </span> <span class="n">foo</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span>
<span class="gp">... </span> <span class="n">default</span><span class="o">=</span><span class="s2">"every attrib needs a type if auto_attribs=True"</span>
<span class="gp">... </span> <span class="p">)</span>
<span class="gp">... </span> <span class="n">bar</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Any</span> <span class="o">=</span> <span class="kc">None</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">AutoC</span><span class="p">)</span><span class="o">.</span><span class="n">l</span><span class="o">.</span><span class="n">type</span>
<span class="go">typing.List[int]</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">AutoC</span><span class="p">)</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">type</span>
<span class="go"><class 'int'></span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">AutoC</span><span class="p">)</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">type</span>
<span class="go"><class 'str'></span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">AutoC</span><span class="p">)</span><span class="o">.</span><span class="n">bar</span><span class="o">.</span><span class="n">type</span>
<span class="go">typing.Any</span>
<span class="gp">>>> </span><span class="n">AutoC</span><span class="p">()</span>
<span class="go">AutoC(l=[], x=1, foo='every attrib needs a type if auto_attribs=True', bar=None)</span>
<span class="gp">>>> </span><span class="n">AutoC</span><span class="o">.</span><span class="n">cls_var</span>
<span class="go">5</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last"><code class="docutils literal"><span class="pre">attrs</span></code> itself doesn’t have any features that work on top of type metadata <em>yet</em>.
However it’s useful for writing your own validators or serialization frameworks.</p>
</div>
</div>
<div class="section" id="slots">
<span id="id2"></span><h2>Slots<a class="headerlink" href="#slots" title="Permalink to this headline">¶</a></h2>
<p>By default, instances of classes have a dictionary for attribute storage.
This wastes space for objects having very few data attributes.
The space consumption can become significant when creating large numbers of instances.</p>
<p>Normal Python classes can avoid using a separate dictionary for each instance of a class by <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#slots">defining</a> <code class="docutils literal"><span class="pre">__slots__</span></code>.
For <code class="docutils literal"><span class="pre">attrs</span></code> classes it’s enough to set <code class="docutils literal"><span class="pre">slots=True</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">slots</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">Coordinates</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal"><span class="pre">attrs</span></code> slot classes can inherit from other classes just like non-slot classes, but some of the benefits of slot classes are lost if you do that.
If you must inherit from other classes, try to inherit only from other slot classes.</p>
</div>
<p>Slot classes are a little different than ordinary, dictionary-backed classes:</p>
<ul>
<li><p class="first">Assigning to a non-existent attribute of an instance will result in an <code class="docutils literal"><span class="pre">AttributeError</span></code> being raised.
Depending on your needs, this might be a good thing since it will let you catch typos early.
This is not the case if your class inherits from any non-slot classes.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">slots</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">Coordinates</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">Coordinates</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">c</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="o">...</span>
<span class="gr">AttributeError</span>: <span class="n">'Coordinates' object has no attribute 'z'</span>
</pre></div>
</div>
</li>
<li><p class="first">Since non-slot classes cannot be turned into slot classes after they have been created, <code class="docutils literal"><span class="pre">attr.s(slots=True)</span></code> will <em>replace</em> the class it is applied to with a copy.
In almost all cases this isn’t a problem, but we mention it for the sake of completeness.</p>
<ul class="simple">
<li>One notable problem is that certain metaclass features like <code class="docutils literal"><span class="pre">__init_subclass__</span></code> do not work with slot classes.</li>
</ul>
</li>
<li><p class="first">Using <a class="reference external" href="/usr/share/doc/python3-doc/html/library/pickle.html#module-pickle" title="(in Python v3.6)"><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a> with slot classes requires pickle protocol 2 or greater.
Python 2 uses protocol 0 by default so the protocol needs to be specified.
Python 3 uses protocol 3 by default.
You can support protocol 0 and 1 by implementing <a class="reference external" href="/usr/share/doc/python3-doc/html/library/pickle.html#object.__getstate__" title="(in Python v3.6)"><code class="xref py py-meth docutils literal"><span class="pre">__getstate__</span></code></a> and <a class="reference external" href="/usr/share/doc/python3-doc/html/library/pickle.html#object.__setstate__" title="(in Python v3.6)"><code class="xref py py-meth docutils literal"><span class="pre">__setstate__</span></code></a> methods yourself.
Those methods are created for frozen slot classes because they won’t pickle otherwise.
<a class="reference external" href="https://www.youtube.com/watch?v=7KnfGDajDQw">Think twice</a> before using <a class="reference external" href="/usr/share/doc/python3-doc/html/library/pickle.html#module-pickle" title="(in Python v3.6)"><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a> though.</p>
</li>
<li><p class="first">As always with slot classes, you must specify a <code class="docutils literal"><span class="pre">__weakref__</span></code> slot if you wish for the class to be weak-referenceable.
Here’s how it looks using <code class="docutils literal"><span class="pre">attrs</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">weakref</span>
<span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">slots</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="vm">__weakref__</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="nb">hash</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="nb">repr</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="nb">cmp</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">weakref</span><span class="o">.</span><span class="n">ref</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="go"><weakref at 0x...; to 'C' at 0x...></span>
</pre></div>
</div>
</li>
</ul>
<p>All in all, setting <code class="docutils literal"><span class="pre">slots=True</span></code> is usually a very good idea.</p>
</div>
<div class="section" id="immutability">
<h2>Immutability<a class="headerlink" href="#immutability" title="Permalink to this headline">¶</a></h2>
<p>Sometimes you have instances that shouldn’t be changed after instantiation.
Immutability is especially popular in functional programming and is generally a very good thing.
If you’d like to enforce it, <code class="docutils literal"><span class="pre">attrs</span></code> will try to help:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">frozen</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">i</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">attr.exceptions.FrozenInstanceError</span>: <span class="n">can't set attribute</span>
<span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">x</span>
<span class="go">1</span>
</pre></div>
</div>
<p>Please note that true immutability is impossible in Python but it will <a class="reference internal" href="how-does-it-work.html#how-frozen"><span class="std std-ref">get</span></a> you 99% there.
By themselves, immutable classes are useful for long-lived objects that should never change; like configurations for example.</p>
<p>In order to use them in regular program flow, you’ll need a way to easily create new instances with changed attributes.
In Clojure that function is called <a class="reference external" href="https://clojuredocs.org/clojure.core/assoc">assoc</a> and <code class="docutils literal"><span class="pre">attrs</span></code> shamelessly imitates it: <a class="reference internal" href="api.html#attr.evolve" title="attr.evolve"><code class="xref py py-func docutils literal"><span class="pre">attr.evolve()</span></code></a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span><span class="p">(</span><span class="n">frozen</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">i1</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">i1</span>
<span class="go">C(x=1, y=2)</span>
<span class="gp">>>> </span><span class="n">i2</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">evolve</span><span class="p">(</span><span class="n">i1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">i2</span>
<span class="go">C(x=1, y=3)</span>
<span class="gp">>>> </span><span class="n">i1</span> <span class="o">==</span> <span class="n">i2</span>
<span class="go">False</span>
</pre></div>
</div>
</div>
<div class="section" id="other-goodies">
<h2>Other Goodies<a class="headerlink" href="#other-goodies" title="Permalink to this headline">¶</a></h2>
<p>Sometimes you may want to create a class programmatically.
<code class="docutils literal"><span class="pre">attrs</span></code> won’t let you down and gives you <a class="reference internal" href="api.html#attr.make_class" title="attr.make_class"><code class="xref py py-func docutils literal"><span class="pre">attr.make_class()</span></code></a> :</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C1</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">C2</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">make_class</span><span class="p">(</span><span class="s2">"C2"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"x"</span><span class="p">,</span> <span class="s2">"y"</span><span class="p">])</span>
<span class="gp">>>> </span><span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C1</span><span class="p">)</span> <span class="o">==</span> <span class="n">attr</span><span class="o">.</span><span class="n">fields</span><span class="p">(</span><span class="n">C2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>You can still have power over the attributes if you pass a dictionary of name: <code class="docutils literal"><span class="pre">attr.ib</span></code> mappings and can pass arguments to <code class="docutils literal"><span class="pre">@attr.s</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">make_class</span><span class="p">(</span><span class="s2">"C"</span><span class="p">,</span> <span class="p">{</span><span class="s2">"x"</span><span class="p">:</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">),</span>
<span class="gp">... </span> <span class="s2">"y"</span><span class="p">:</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="n">attr</span><span class="o">.</span><span class="n">Factory</span><span class="p">(</span><span class="nb">list</span><span class="p">))},</span>
<span class="gp">... </span> <span class="nb">repr</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">i</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">i</span> <span class="c1"># no repr added!</span>
<span class="go"><__main__.C object at ...></span>
<span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">x</span>
<span class="go">42</span>
<span class="gp">>>> </span><span class="n">i</span><span class="o">.</span><span class="n">y</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>If you need to dynamically make a class with <a class="reference internal" href="api.html#attr.make_class" title="attr.make_class"><code class="xref py py-func docutils literal"><span class="pre">attr.make_class()</span></code></a> and it needs to be a subclass of something else than <code class="docutils literal"><span class="pre">object</span></code>, use the <code class="docutils literal"><span class="pre">bases</span></code> argument:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">D</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="kc">True</span> <span class="c1"># arbitrary example</span>
<span class="gp">>>> </span><span class="n">C</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">make_class</span><span class="p">(</span><span class="s2">"C"</span><span class="p">,</span> <span class="p">{},</span> <span class="n">bases</span><span class="o">=</span><span class="p">(</span><span class="n">D</span><span class="p">,),</span> <span class="nb">cmp</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">C</span><span class="p">(),</span> <span class="n">D</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Sometimes, you want to have your class’s <code class="docutils literal"><span class="pre">__init__</span></code> method do more than just
the initialization, validation, etc. that gets done for you automatically when
using <code class="docutils literal"><span class="pre">@attr.s</span></code>.
To do this, just define a <code class="docutils literal"><span class="pre">__attrs_post_init__</span></code> method in your class.
It will get called at the end of the generated <code class="docutils literal"><span class="pre">__init__</span></code> method.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">y</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">z</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="n">init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__attrs_post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span>
<span class="gp">>>> </span><span class="n">obj</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">obj</span>
<span class="go">C(x=1, y=2, z=3)</span>
</pre></div>
</div>
<p>Finally, you can exclude single attributes from certain methods:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nd">@attr</span><span class="o">.</span><span class="n">s</span>
<span class="gp">... </span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">user</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">password</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">ib</span><span class="p">(</span><span class="nb">repr</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">C</span><span class="p">(</span><span class="s2">"me"</span><span class="p">,</span> <span class="s2">"s3kr3t"</span><span class="p">)</span>
<span class="go">C(user='me')</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/attrs_logo.svg" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#"><code class="docutils literal"><span class="pre">attrs</span></code> by Example</a><ul>
<li><a class="reference internal" href="#basics">Basics</a></li>
<li><a class="reference internal" href="#converting-to-collections-types">Converting to Collections Types</a></li>
<li><a class="reference internal" href="#defaults">Defaults</a></li>
<li><a class="reference internal" href="#validators">Validators</a><ul>
<li><a class="reference internal" href="#decorator">Decorator</a></li>
<li><a class="reference internal" href="#callables">Callables</a></li>
</ul>
</li>
<li><a class="reference internal" href="#conversion">Conversion</a></li>
<li><a class="reference internal" href="#metadata">Metadata</a></li>
<li><a class="reference internal" href="#types">Types</a></li>
<li><a class="reference internal" href="#slots">Slots</a></li>
<li><a class="reference internal" href="#immutability">Immutability</a></li>
<li><a class="reference internal" href="#other-goodies">Other Goodies</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="why.html" title="previous chapter">Why not…</a></li>
<li>Next: <a href="api.html" title="next chapter">API Reference</a></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2018, Hynek Schlawack.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.7</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
</div>
</body>
</html>
|