Source

python-peps / pep-3133.txt

The default branch has multiple heads

  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
PEP: 3133
Title: Introducing Roles
Version: $Revision$
Last-Modified: $Date$
Author: Collin Winter <collinwinter@google.com>
Status: Rejected
Type: Standards Track
Content-Type: text/x-rst
Requires: 3115, 3129
Created: 1-May-2007
Python-Version: 3.0
Post-History: 13-May-2007


Rejection Notice
================

This PEP has helped push PEP 3119 towards a saner, more minimalistic
approach.  But given the latest version of PEP 3119 I much prefer
that.  GvR.


Abstract
========

Python's existing object model organizes objects according to their
implementation.  It is often desirable -- especially in
duck typing-based language like Python -- to organize objects by
the part they play in a larger system (their intent), rather than by
how they fulfill that part (their implementation).  This PEP
introduces the concept of roles, a mechanism for organizing
objects according to their intent rather than their implementation.


Rationale
=========

In the beginning were objects.  They allowed programmers to marry
function and state, and to increase code reusability through concepts
like polymorphism and inheritance, and lo, it was good.  There came
a time, however, when inheritance and polymorphism weren't enough.
With the invention of both dogs and trees, we were no longer able to
be content with knowing merely, "Does it understand 'bark'?"
We now needed to know what a given object thought that "bark" meant.

One solution, the one detailed here, is that of roles, a mechanism
orthogonal and complementary to the traditional class/instance system.
Whereas classes concern themselves with state and implementation, the
roles mechanism deals exclusively with the behaviours embodied in a
given class.

This system was originally called "traits" and implemented for Squeak
Smalltalk [#traits-paper]_.  It has since been adapted for use in
Perl 6 [#perl6-s12]_ where it is called "roles", and it is primarily
from there that the concept is now being interpreted for Python 3.
Python 3 will preserve the name "roles".

In a nutshell: roles tell you *what* an object does, classes tell you
*how* an object does it.

In this PEP, I will outline a system for Python 3 that will make it
possible to easily determine whether a given object's understanding
of "bark" is tree-like or dog-like.  (There might also be more
serious examples.)


A Note on Syntax
----------------

A syntax proposals in this PEP are tentative and should be
considered to be strawmen.  The necessary bits that this PEP depends
on -- namely PEP 3115's class definition syntax and PEP 3129's class
decorators -- are still being formalized and may change.  Function
names will, of course, be subject to lengthy bikeshedding debates.


Performing Your Role
====================

Static Role Assignment
----------------------

Let's start out by defining ``Tree`` and ``Dog`` classes ::

  class Tree(Vegetable):
    
    def bark(self):
      return self.is_rough()


  class Dog(Animal):
    
    def bark(self):
      return self.goes_ruff()

While both implement a ``bark()`` method with the same signature,
they do wildly different things.  We need some way of differentiating
what we're expecting. Relying on inheritance and a simple
``isinstance()`` test will limit code reuse and/or force any dog-like
classes to inherit from ``Dog``, whether or not that makes sense.
Let's see if roles can help. ::
    
  @perform_role(Doglike)
  class Dog(Animal):
    ...

  @perform_role(Treelike)
  class Tree(Vegetable):
    ...
    
  @perform_role(SitThere)
  class Rock(Mineral):
    ...
    
We use class decorators from PEP 3129 to associate a particular role
or roles with a class.  Client code can now verify that an incoming
object performs the ``Doglike`` role, allowing it to handle ``Wolf``,
``LaughingHyena`` and ``Aibo`` [#aibo]_ instances, too.

Roles can be composed via normal inheritance: ::

  @perform_role(Guard, MummysLittleDarling)
  class GermanShepherd(Dog):
    
    def guard(self, the_precious):
      while True:
        if intruder_near(the_precious):
          self.growl()
          
    def get_petted(self):
      self.swallow_pride()

Here, ``GermanShepherd`` instances perform three roles: ``Guard`` and
``MummysLittleDarling`` are applied directly, whereas ``Doglike``
is inherited from ``Dog``.


Assigning Roles at Runtime
--------------------------

Roles can be assigned at runtime, too, by unpacking the syntactic
sugar provided by decorators.

Say we import a ``Robot`` class from another module, and since we
know that ``Robot`` already implements our ``Guard`` interface,
we'd like it to play nicely with guard-related code, too. ::

  >>> perform(Guard)(Robot)

This takes effect immediately and impacts all instances of ``Robot``.


Asking Questions About Roles
----------------------------

Just because we've told our robot army that they're guards, we'd
like to check in on them occasionally and make sure they're still at
their task. ::

  >>> performs(our_robot, Guard)
  True
   
What about that one robot over there? ::

  >>> performs(that_robot_over_there, Guard)
  True

The ``performs()`` function is used to ask if a given object
fulfills a given role.  It cannot be used, however, to ask a
class if its instances fulfill a role: ::

  >>> performs(Robot, Guard)
  False
 
This is because the ``Robot`` class is not interchangeable
with a ``Robot`` instance.


Defining New Roles
==================

Empty Roles
-----------

Roles are defined like a normal class, but use the ``Role``
metaclass. ::

  class Doglike(metaclass=Role):
    ...

Metaclasses are used to indicate that ``Doglike`` is a ``Role`` in
the same way 5 is an ``int`` and ``tuple`` is a ``type``.


Composing Roles via Inheritance
-------------------------------

Roles may inherit from other roles; this has the effect of composing
them.  Here, instances of ``Dog`` will perform both the
``Doglike`` and ``FourLegs`` roles. ::

  class FourLegs(metaclass=Role):
    pass

  class Doglike(FourLegs, Carnivor):
    pass
 
  @perform_role(Doglike)  
  class Dog(Mammal):
    pass


Requiring Concrete Methods
--------------------------

So far we've only defined empty roles -- not very useful things.
Let's now require that all classes that claim to fulfill the
``Doglike`` role define a ``bark()`` method: ::

  class Doglike(FourLegs):

    def bark(self):
      pass
      
No decorators are required to flag the method as "abstract", and the
method will never be called, meaning whatever code it contains (if any)
is irrelevant.  Roles provide *only* abstract methods; concrete
default implementations are left to other, better-suited mechanisms
like mixins.

Once you have defined a role, and a class has claimed to perform that
role, it is essential that that claim be verified.  Here, the
programmer has misspelled one of the methods required by the role. ::

  @perform_role(FourLegs)
  class Horse(Mammal):

    def run_like_teh_wind(self)
      ...
      
This will cause the role system to raise an exception, complaining
that you're missing a ``run_like_the_wind()`` method.  The role
system carries out these checks as soon as a class is flagged as
performing a given role.

Concrete methods are required to match exactly the signature demanded
by the role.  Here, we've attempted to fulfill our role by defining a
concrete version of ``bark()``, but we've missed the mark a bit. ::

  @perform_role(Doglike)
  class Coyote(Mammal):
    
    def bark(self, target=moon):
      pass

This method's signature doesn't match exactly with what the
``Doglike`` role was expecting, so the role system will throw a bit
of a tantrum.


Mechanism
=========

The following are strawman proposals for how roles might be expressed
in Python.  The examples here are phrased in a way that the roles
mechanism may be implemented without changing the Python interpreter.
(Examples adapted from an article on Perl 6 roles by Curtis Poe
[#roles-examples]_.)

1. Static class role assignment ::

     @perform_role(Thieving)
     class Elf(Character):
       ...
       
   ``perform_role()`` accepts multiple arguments, such that this is
   also legal: ::
   
     @perform_role(Thieving, Spying, Archer)
     class Elf(Character):
       ...
       
   The ``Elf`` class now performs both the ``Thieving``, ``Spying``,
   and ``Archer`` roles.   

2. Querying instances ::

     if performs(my_elf, Thieving):
       ...
       
   The second argument to ``performs()`` may also be anything with a
   ``__contains__()`` method, meaning the following is legal: ::
   
     if performs(my_elf, set([Thieving, Spying, BoyScout])):
       ...
       
   Like ``isinstance()``, the object needs only to perform a single
   role out of the set in order for the expression to be true.


Relationship to Abstract Base Classes
=====================================

Early drafts of this PEP [#proposal]_ envisioned roles as competing
with the abstract base classes proposed in PEP 3119.  After further
discussion and deliberation, a compromise and a delegation of
responsibilities and use-cases has been worked out as follows:

* Roles provide a way of indicating a object's semantics and abstract
  capabilities.  A role may define abstract methods, but only as a
  way of delineating an interface through which a particular set of
  semantics are accessed.  An ``Ordering`` role might require that
  some set of ordering operators  be defined. ::
 
    class Ordering(metaclass=Role):
      def __ge__(self, other):
        pass
    
      def __le__(self, other):
        pass
        
      def __ne__(self, other):
        pass
      
      # ...and so on
      
  In this way, we're able to indicate an object's role or function
  within a larger system without constraining or concerning ourselves
  with a particular implementation.
      
* Abstract base classes, by contrast, are a way of reusing common,
  discrete units of implementation.  For example, one might define an
  ``OrderingMixin`` that implements several ordering operators in
  terms of other operators. ::
 
    class OrderingMixin:
      def __ge__(self, other):
        return self > other or self == other
    
      def __le__(self, other):
        return self < other or self == other
    
      def __ne__(self, other):
        return not self == other
      
      # ...and so on
    
  Using this abstract base class - more properly, a concrete
  mixin - allows a programmer to define a limited set of operators
  and let the mixin in effect "derive" the others.

By combining these two orthogonal systems, we're able to both
a) provide functionality, and b) alert consumer systems to the
presence and availability of this functionality.  For example,
since the ``OrderingMixin`` class above satisfies the interface
and semantics expressed in the ``Ordering`` role, we say the mixin
performs the role: ::

  @perform_role(Ordering)
  class OrderingMixin:
    def __ge__(self, other):
      return self > other or self == other

    def __le__(self, other):
      return self < other or self == other

    def __ne__(self, other):
      return not self == other

    # ...and so on

Now, any class that uses the mixin will automatically -- that is,
without further programmer effort -- be tagged as performing the
``Ordering`` role.

The separation of concerns into two distinct, orthogonal systems
is desirable because it allows us to use each one separately.
Take, for example, a third-party package providing a
``RecursiveHash`` role that indicates a container takes its
contents into account when determining its hash value.  Since
Python's built-in ``tuple`` and ``frozenset`` classes follow this
semantic, the ``RecursiveHash`` role can be applied to them. ::

  >>> perform_role(RecursiveHash)(tuple)
  >>> perform_role(RecursiveHash)(frozenset)

Now, any code that consumes ``RecursiveHash`` objects will now be
able to consume tuples and frozensets.


Open Issues
===========

Allowing Instances to Perform Different Roles Than Their Class
--------------------------------------------------------------

Perl 6 allows instances to perform different roles than their class.
These changes are local to the single instance and do not affect
other instances of the class.  For example: ::

  my_elf = Elf()
  my_elf.goes_on_quest()
  my_elf.becomes_evil()
  now_performs(my_elf, Thieving) # Only this one elf is a thief
  my_elf.steals(["purses", "candy", "kisses"])

In Perl 6, this is done by creating an anonymous class that
inherits from the instance's original parent and performs the
additional role(s).  This is possible in Python 3, though whether it
is desirable is still is another matter.

Inclusion of this feature would, of course, make it much easier to
express the works of Charles Dickens in Python: ::

  >>> from literature import role, BildungsRoman
  >>> from dickens import Urchin, Gentleman
  >>>
  >>> with BildungsRoman() as OliverTwist:
  ...   mr_brownlow = Gentleman()
  ...   oliver, artful_dodger = Urchin(), Urchin()
  ...   now_performs(artful_dodger, [role.Thief, role.Scoundrel])
  ...
  ...   oliver.has_adventures_with(ArtfulDodger)
  ...   mr_brownlow.adopt_orphan(oliver)
  ...   now_performs(oliver, role.RichWard)


Requiring Attributes
--------------------

Neal Norwitz has requested the ability to make assertions about
the presence of attributes using the same mechanism used to require
methods.  Since roles take effect at class definition-time, and
since the vast majority of attributes are defined at runtime by a
class's ``__init__()`` method, there doesn't seem to be a good way
to check for attributes at the same time as methods.

It may still be desirable to include non-enforced attributes in the
role definition, if only for documentation purposes.


Roles of Roles
--------------

Under the proposed semantics, it is possible for roles to
have roles of their own. ::

  @perform_role(Y)
  class X(metaclass=Role):
    ...

While this is possible, it is meaningless, since roles
are generally not instantiated.  There has been some
off-line discussion about giving meaning to this expression, but so
far no good ideas have emerged.


class_performs()
----------------

It is currently not possible to ask a class if its instances perform
a given role.  It may be desirable to provide an analogue to
``performs()`` such that ::

  >>> isinstance(my_dwarf, Dwarf)
  True
  >>> performs(my_dwarf, Surly)
  True
  >>> performs(Dwarf, Surly)
  False
  >>> class_performs(Dwarf, Surly)
  True
 

Prettier Dynamic Role Assignment
--------------------------------

An early draft of this PEP included a separate mechanism for
dynamically assigning a role to a class.  This was spelled ::

  >>> now_perform(Dwarf, GoldMiner)
 
This same functionality already exists by unpacking the syntactic
sugar provided by decorators: ::

  >>> perform_role(GoldMiner)(Dwarf)
 
At issue is whether dynamic role assignment is sufficiently important
to warrant a dedicated spelling.


Syntax Support
--------------

Though the phrasings laid out in this PEP are designed so that the
roles system could be shipped as a stand-alone package, it may be
desirable to add special syntax for defining, assigning and
querying roles.  One example might be a role keyword, which would
translate ::

  class MyRole(metaclass=Role):
    ...
    
into ::

  role MyRole:
    ...
    
Assigning a role could take advantage of the class definition
arguments proposed in PEP 3115: ::

  class MyClass(performs=MyRole):
    ...


Implementation
==============

A reference implementation is forthcoming.


Acknowledgements
================

Thanks to Jeffery Yasskin, Talin and Guido van Rossum for several
hours of in-person discussion to iron out the differences, overlap
and finer points of roles and abstract base classes.


References
==========

.. [#aibo]
   http://en.wikipedia.org/wiki/AIBO

.. [#roles-examples]
   http://www.perlmonks.org/?node_id=384858
   
.. [#perl6-s12]
   http://dev.perl.org/perl6/doc/design/syn/S12.html
   
.. [#traits-paper]
   http://www.iam.unibe.ch/~scg/Archive/Papers/Scha03aTraits.pdf
   
.. [#proposal]
   http://mail.python.org/pipermail/python-3000/2007-April/007026.html


Copyright
=========

This document has been placed in the public domain.



..
   Local Variables:
   mode: indented-text
   indent-tabs-mode: nil
   sentence-end-double-space: t
   fill-column: 70
   coding: utf-8
   End:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.