4

I recently came across a very interesting talk by James Powell on YouTube (LINK, around 12 minutes into the video). In his PyData talk, he states that "[...] The Python data model is a means by which you can implement protocols [...]". He relates his statement to the use of dunder-methods (like __init__, __repr__, etc.).

I've been programming for quite a while with Python, but I haven't come across POP. The video didn't really define what it is. What is POP in Python actually and how is it used in practice?

5
  • Wikipedia. Note how Python isn't mentioned there. Also read this. POP isn't available in Python.
    – Nearoo
    Aug 17, 2018 at 12:25
  • @Nearoo: I also found that site, but I thought it could possibly be outdated. I just wanted to make sure that I'm not missing a new feature or something.
    – DocDriven
    Aug 17, 2018 at 13:09
  • Actaully, now I see what you're looking for. POP generally refers to the usage of the interface concept. An interface is like a class that only declares method signatures and defines what the implementation of these methods should do (here's an example of Java's widely used comparable interface). In a way, you can look at methods like __repr__, __cmp__ etc. as interface implementations. Any object that has __cmp__ can be compared - in Java, you'd make the object implement the Comparable interface.
    – Nearoo
    Aug 17, 2018 at 14:28
  • But unlike in Java, you can't define new interfaces in Python.
    – Nearoo
    Aug 17, 2018 at 14:28
  • Since you don't define variable types in Python, the concept of interfaces doesn't have any benefit anyway. In Python, if you write a method that requires an Object that implements a method with a certain signature, you simply note that in the documentation of that method and rely on the client to follow that.
    – Nearoo
    Aug 17, 2018 at 14:33

2 Answers 2

3

By python being protocol oriented, he may mean the combination of the following:

  1. Duck typing: "If it walks like a duck and it quacks like a duck, then it must be a duck". In contrast to languages like C or C++, you don't care so much about the type of an object you are passing around but rather the interface the object provides. I see the object interface as equivalent to the sum of its methods or the protocol the object provides. Although typing is now part of python since version 3.5 through type hints, duck typing is so deeply ingrained into the language that PEP 544 – fittingly called "Protocols: Structural subtyping (static duck typing)" – embeds that very concept into the language while using static typing.

  2. Methods (== protocol) are pythons means of meeting its operators, standard functions and even its syntax:

    • To use an operator like a + b you implement the __add__(self, other) method for a
    • To meet the requirements of a Sized type thus providing the standard len(…) function, you implement the __len__ method
    • To use object a in a for loop like for _ in a:, you implement its __iter__ method
    • To use object an object in a generator expression, you implement also its __next__ method
    • To enable the syntax for usage in a context with A(…) as a: you provide __enter__ and __exit__ methods

    We as programmers serve python's standard means of inclusion by providing the corresponding protocol for an object.

  3. In python, everything is an object, strings, lists, functions and even modules. The paradigms shown above are applicable there, too. You can in principle add functions, which are first class objects. Functions itself have methods, which are objects, so they have methods, which are objects, ….

Additional note: I found your question after watching the same talk and stumbling over the same phrase. My impression is he didn't meant the term “protocol-oriented” literally but more colloquially. I think the three items above justify his phrase.

-3

Are you sure about pop I think you mean oop Object oriented programming Like this:

Class animal:
     def __init__(self, name, color):
          self.name = name
          self.color = color

Dog = animal(" huskey",  "black")
print(animal.name)
print(animal.color)
1
  • Thanks for your response, but I really mean Protocol-Oriented Programming. That confused me at first, too.
    – DocDriven
    Aug 17, 2018 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.