python @property @staticmethod
PHP中文网
PHP中文网 2017-04-18 09:04:34
0
1
814

这两个东西在什么情况下用

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
伊谢尔伦

Simply put

  • @property 是 Python 中用來取代 getter/setter 的 decorator,他可以包裝、處理、封裝類屬性,也可以很方便地建立唯讀屬性,重點是他維持了 介面的簡單。因為我們能夠很直覺的存取屬性而不必使用冗贅地使用 get_XXXset_XXX This type of getter/setter. I originally wanted to write a complete explanation, but after thinking about it, I felt that it was not comprehensive enough, so I gave up and waited to complete it in the future. If there are other experts who know this area well, please feel free to give the questioner and me some opinions.

  • @staticmethod I think it is a decorator that has no practical use and is unnecessary (if you have different opinions on this, please feel free to discuss it, I really want to discuss this)


(This answer is a little more difficult. If you find it difficult to read in Traditional Chinese, you can use your browser to convert it to Simplified Chinese and read it...)

@staticmethod

Features and usage situations

Let’s talk about it firststaticmethod 好了,被 staticmethod 修飾的 method 並不會接收特殊的第一項引數 (一般的 instance method 和被 classmethod The modified category method will accept the reference of the instance and category as the first parameter respectively), which makes the static method like a general function, but it happens to be defined in the class instead of directly in module level.

So his usage scenario is: when a function in a class does not need a reference to an instance or class such as self 或是 cls, using static methods can complete the work more concisely and efficiently.

  1. The concise part is that you don’t need to receive an extra irrelevant argument

  2. The efficiency lies in the fact that the general instance method is a bound method (an object) and is generated when we want to use it. This will cost a little more, but the static method does not

Practical/not practical?

But I think it is not practical. First of all, we can use the upper-level method of classmethod 就好,在代碼之中我們依靠第一項引數提供的 class 參考能夠完成與類有關的操作 (比如說 __init__ 的替代物 或是 做為一個調度更多子 staticmethod for functions that are directly related to the class).

If it is not related to the class (and not related to the instance), then we need the purpose of class static method. I can only think of one for the moment: to have an abstract level in the call (yes, I am talking about namespace ) Although this function does not directly contact the class, it is related to the class.

But in Python, using namespace is very easy. We might as well define the function that we originally wanted to define as a static method at the module level and place it as close to the relevant category as possible. I think it is enough ( Perspectives of Luciano Ramalho).

Reflection

Just after I finished typing the above paragraph, I specifically read an excellent article by Julien Danjou:
The definitive guide on how to use static, class or abstract methods in Python
I highly recommend it to anyone who wants to figure it out. Python method people should read this article.

This article puts forward two advantages staticmethod. The first point I have already mentioned above is that static methods are more concise and efficient than instance methods. The second point is that static methods have nothing to do with classes but belong to classes , which means that they can customize services for classes. This is a bit difficult to understand, let’s look at an example (example in Julien Danjou’s article):

class Pizza(object):
    @staticmethod
    def mix_ingredients(x, y):
        return x + y
 
    def cook(self):
        return self.mix_ingredients(self.cheese, self.vegetables)

Let’s imagine if we took mix_ingredients 定義在 module level,那當我們在處理繼承 Pizza 的子類時,勢必無法藉由改動 mix_ingredients 來更動 mix ingredients 的行為 (因為該函數有別的類在使用),那我們只好複寫 cook.

This reason slightly changed my mind. At least he pointed out the biggest difference between a static method and a general method. Static methods belong to a certain category.

But I still stick to my original idea, because maybe mix_ingredients 可以寫得更好,又或者是對於 Pizza 這種非抽象類別根本是不要去繼承他的,又或是繼承的時候複寫方法是不好的,甚至我可以覺得更動 mix_ingredients 跟更動 cook it’s also a burden.

Summary

All of the above I think , maybe one day staticmethod I can really put it to use gracefully, I may admit my ignorance and superficial understanding today. As for you? I think you can have your own ideas. As long as you know enough and can convince yourself, then I don’t think any position is wrong :)


Questions I answered: Python-QA

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template