Does Python have C # / Java interfaces?

I worked for several months as a C # programmer and got used to the idea of generics / templates interfaces, which I could pass to the library without worrying about how the object was created.

I'm going to start with a relatively large project, perhaps in python (I used to write a lot of python, but mostly my own code for data analysis, etc.), and wondered if there is a similar concept in this language? I tried to walk for him and did not come up with a lot.

If the answer is no, that's good, but in this case, what do people usually do?

+6
source share
5 answers

My understanding of interfaces is from Java, so hopefully this is close enough to what you mean ...

There is no syntactic way to claim that a particular object has a set of methods and fields, because Python uses dynamic typing / duck printing . If you want to check if an object has a specific method or field, you can:

  • Fall into it with reflection (this works most)
  • Check what you want in __dict__ (a dictionary of all participants in the object). MyObj.__dict__.hasKey(o) is what you want, I think. (pretty easy)
  • Just use the object and trust that you, your fellow developers or your users have provided you with a suitable object. (As light as possible, but dangerous)
  • Or do it above and wrap it in a try / except block. (This will be familiar to Java programmers)

Designing a complex set of classes in Python is a completely different experience from doing the same thing in C-type languages, so be prepared to do what may not be convenient for you (e.g. option 3 above). Good luck

+5
source

If the answer is no, that's good, but in this case, what do people usually do?

Duck typing .

What's important is to approach Python by dumping C # technical baggage.

Learn Python as a new language. Do not try to display concepts between Python and C #. So madness.


"INTERFACES, not generics or templates"

Irrelevant. All this technology of declaring a static type is not required. You also do not need to use casting to break the rules.

+15
source

python is dynamically typed so (in most cases) there is no need for generics / templates.

+5
source

If you are talking strictly about a C # / Java-like interface, then the answer is simple. These are concepts that relate to statically typed languages ​​and do not actually apply to a dynamic language such as Python.

The usual pythonic approach is to trust the objects you get and assume that they have the necessary traits that you need. If this is not the case, you can expect runtime errors that can be processed or left to the caller. This is consistent with the general dynamic nature of the language: if something goes wrong, you can expect it to display at runtime.

Java or C # interfaces, on the other hand, are means of checking the necessary preconditions (i.e., objects that have certain methods) at compile time. This (possibly) removes some runtime flexibility to increase the security of compile-time checks.

Note that this does not mean that the concept of any interface does not have a place in a dynamically typed language. In fact, Python has a slightly vague concept of β€œmeta types” - such as iterators - that are often checked against, for example, by checking for the presence of specific method (s):

 def process_sequence(seq): if not hasattr(seq, '__iter__'): seq = [seq] # turn single element into iterable for elem in seq: process_element(elem) 

This is conceptually similar to implicit interfaces from the Go language. If you want to have your own iterative option, you simply implement the __iter__ method - without explicitly indicating that you implement it (as in Java, where you inherit the List class). If someone wants to check if your object is really iterable, he can check if the β€œcontract” is actually executed, i.e. Having a __iter__ method. This is what andronicus described in his answer.

As a shameless plugin, I can point to the pyduck library that I am implementing. It aims to simplify (and possibly even standardize) the verification of such "contracts" (through reflection) and makes it somewhat more reliable.

+4
source

zope.interface

But most people do not use it.

+3
source

Source: https://habr.com/ru/post/898157/


All Articles