Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to learn the Python interface from zope.interface

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand Python interface from zope.interface". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand Python interface from zope.interface".

Implicit Interface is not the Zen of Python

The Python Zen is loose, but a bit contradictory, so much so that you can use it to illustrate anything. Let's consider one of the most famous principles: "it is better to show than implicitly."

Traditionally, one of the things implied in Python is the expected interface. For example, the function has recorded that it expects a "class file object" or "sequence". But what is a class file object? Does it support .writelines? Where are the .girls? What is a "sequence"? Do you support step slicing, such as a [1:10:2]?

At first, Python's answer was the so-called "duck type", taken from the phrase "if it walks like a duck and quacks like a duck, then it may be a duck." In other words, "give it a try." this is probably the most implicit expression you can get.

In order for this to be expressed explicitly, you need a way to express the desired interface. The Zope Web framework was one of the first large systems written in Python, and it desperately needed these things to make the code explicit, such as what to expect from "user-like objects."

Zope.interface is developed by Zope but is released as a separate Python package. Zope.interface can help declare which interfaces exist, which objects are provided, and how to query this information.

Imagine writing a simple 2D game that requires a variety of things to support the LCTT interface. For example, represents a bounding box, but also indicates when the object intersects a box. Unlike some other languages, it is a common practice in Python to have property access as part of a public interface, rather than implementing getter and setter. The bounding box should be a property, not a method.

The way to render a list of sprites might be similar to:

Def render_sprites (render_surface, sprites): "" sprites should be a list of objects that conform to the Sprite interface: * an attribute named "bounding_box" that contains a bounding box * a method called "intersects" that accepts a bounding box and returns True or False "" pass # some code for actual rendering

The game will have many functions for dealing with sprites. In each function, you must specify the expectation in the accompanying document.

In addition, some functions may expect to use more complex sprite objects, such as those with Z order. We must keep track of which methods require Sprite objects and which methods require SpriteWithZ objects.

Wouldn't it be nice if you could make sprites explicit and intuitive so that you can declare "I need a sprite" and have a strictly defined interface? Take a look at zope.interface.

From zope import interface class ISprite (interface.Interface): bounding_box = interface.Attribute ("bounding box") def intersects (box): "does it intersect with a box?"

At first glance, this code looks a little strange. These methods do not include self, but it is a common practice to include self, and it has an attribute. This is the way to declare an interface in zope.interface. This seems strange because most people are not used to strictly declaring interfaces.

The reason for this is that the interface shows how the method is called, not how the method is defined. Because interfaces are not superclasses, they can be used to declare data properties.

Here is an implementation of an interface that can have circular sprites:

Implementer (ISprite) @ attr.s (auto_attribs=True) class CircleSprite: X: float y: float radius: float @ property def bounding_box (self): return (self.x-self.radius, self.y-self.radius, self.x + self.radius, self.y + self.radius,) def intersects (self Box): # if and only if at least one corner is in the circle The box intersects the circle top_left, bottom_right = box [: 2], box [2:] for choose_x_from (top_left, bottom_right): for choose_y_from (top_left Bottom_right): X = choose_x_from [0] y = choose_y_from [1] if ((x-self.x) `2 + (y-self.y) `2)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report