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

What is the purpose of the Python class definition

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what is the function of Python class definition". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The Class Scope

The scope of Ruby class has the following functions:

Define the method. Provide a method definition when looking for a method for its instance (or an instance of its subclass)

A container for storing constants. You can access constants through ClassName::CONST syntax

As a common scope, it can also store local variables and so on. But cannot be directly accessed by the outside world.

Judging from these functions, there seems to be no difference between Ruby and Python's class scope. It's just that Python doesn't give special treatment to constants. However, the following differences are the key points:

Before-vs- After

When you can write code in Ruby's class scope, the class itself is instantiated. You can call the method of this class during the process of defining class. Or, in the way of Smalltalk: send a message to class. That's why we can see simple and elegant DSL like Sinatra and Rails Model.

By contrast, the class scope of Python exists before class is instantiated, and at the end of the definition, the dictionary scope is passed to metaclass along with the tuple that declares the superclass to complete the construction of the class.

If Python wants to do something as simple as attr_accessor: foo,: bar, at best, it can only introduce a special property descriptor constructor at the model level, and it has to repeat yourself in order to fill the scope. I'm still afraid to use the bad scheme of introspecting the scope of call stack modification. It's too much to type locals () explicitly every time. So don't expect Python to design an elegant and practical internal DSL based on OO in your lifetime.

Reopening Class

Another big advantage of Ruby class: it can be reopened and modified at any time. So you can add or subtract the old class, and you can add mixin to it. Here is a small caveat: the local variables defined in Class scope are independent of each reopen and cannot be accessed each other.

Here Python can only be tragic: the multi-inheritance relationship and the initial class scope are defined by metaclass at one time, and there is no legitimate opportunity to modify them. Deleting the old method definition is simple: del cls.method. Adding methods is laborious: without code block syntax, lambda expressions can't write multiple statements, and don't forget to leave the position of the first parameter as self.

So it can be generally asserted that Python's class is basically static after definition. It is not convenient and it is not recommended to modify its definition with dark magic.

Decorator-vs- Redefining Method

Decorator is a powerful presence in Python syntax and is fully used by many modern library. It is also often seen in class definitions. Such as the built-in property ().

However, decorator plays a very small role in defining classes and is not much better than property descriptor constructor. After all, decorator is designed for function composition, not OOP.

Among the built-in decorator, staticmethod / classmethod is irreplaceable but very annoying. The defined methods appear on both the instance and class property lookup routes.

Ruby does not have decorator, but you can take advantage of the previously mentioned class scope persistence feature to take out the old method definition, wrap it, and then redefine it. See how filter is declared in Rails Controller.

In addition, someone has actually implemented a scaled-down version of decorator in Ruby. But it doesn't seem to be widely used.

Class of Class

There is a special metaclass in Python to express "class that can be instantiated to produce class". Ruby does not have this special concept. But in practice, Python's metaclass is more like decorator: preprocess the contents of the superclass tuple and class scope, and finally pass the processed content to type ().

Another noteworthy problem is that Metaclass builds the generated class, and the process of finding its attributes may be confused.

By contrast, this language ability also exists in Ruby but is rarely used. Most of the practical scenarios for Metaclass are to "define templated class according to the requirements of the library / framework". This requirement can be easily achieved with inherited / included hook plus the aforementioned capabilities of reopening class. Not much.

This is the end of the content of "what is the purpose of Python class definition". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report