python中的底線

來源:互聯網
上載者:User

標籤:

Difference between _, __ and __xx__ in Python

許多人在學習python的時候並沒有真正理解底線的意義,下面給出詳細的解釋。

One underline in the beginning

Python doesn‘t have real private methods, so one underline in the beginning of a method or attribute means you shouldn‘t access this method, because it‘s not part of the API. It‘s very common when using properties:

Python沒有像C++那樣的私人域,使用一個底線開頭的方法或屬性工作表示你無法通過對象名或類名訪問該方法或屬性,即該方法或屬性是private的,對外部不可見。

對於properties同樣適用。

class BaseForm(StrAndUnicode):    ...        def _get_errors(self):        "Returns an ErrorDict for the data provided for the form"        if self._errors is None:            self.full_clean()        return self._errors        errors = property(_get_errors)

This snippet was taken from django source code (django/forms/forms.py). This means errors is a property, and it‘s part of the API, but the method this property calls, _get_errors, is "private", so you shouldn‘t access it.

Two underlines in the beginning

This one causes a lot of confusion. It should not be used to mark a method as private, the goal here is to avoid your method to be overridden by a subclass. Let‘s see an example:

雙底線開頭的方法是為了防止子類重寫該方法。

class A(object):    def __method(self):        print "I‘m a method in A"        def method(self):        self.__method()     a = A()a.method()

輸出如下:

$ python example.py I‘m a method in A

輸出沒問題,現在看下子類也有同樣的__method的情況。

class B(A):    def __method(self):        print "I‘m a method in B"b = B()b.method()

猜猜輸出是什嗎?...

$ python example.pyI‘m a method in A

as you can see, A.method() didn‘t call B.__method() as we could expect. Actually this is the correct behavior for __. So when you create a method starting with __ you‘re saying that you don‘t want anybody to override it, it will be accessible just from inside the own class.

How python does it? Simple, it just renames the method. Take a look:

a = A()a._A__method()  # never use this!! please!
$ python example.pyI‘m a method in A

If you try to access a.__method() it won‘t work either, as I said, __method is just accessible inside the class itself.

Two underlines in the beginning and in the end

When you see a method like __this__, the rule is simple: don‘t call it. Why? Because it means it‘s a method python calls, not you. Take a look:

>>> name = "igor">>> name.__len__()4>>> len(name)4>>> number = 10>>> number.__add__(20)30>>> number + 2030

There is always an operator or native function that calls these magic methods. The idea here is to give you the ability to override operators in your own classes. Sometimes it‘s just a hook python calls in specific situations. __init__(), for example, is called when the object is created so you can initialize it. __new__() is called to build the instance, and so on...

Here‘s an example:

class CrazyNumber(object):        def __init__(self, n):        self.n = n        def __add__(self, other):        return self.n - other        def __sub__(self, other):        return self.n + other        def __str__(self):        return str(self.n)
num = CrazyNumber(10)print num # 10print num + 5 # 5print num - 20 # 30

Another example:

class Room(object):    def __init__(self):        self.people = []    def add(self, person):        self.people.append(person)    def __len__(self):        return len(self.people)room = Room()room.add("Igor")print len(room)     # 1

The documentation covers all these special methods.

Conclusion

Use _one_underline to mark you methods as not part of the API. Use__two_underlines__ when you‘re creating objects to look like native python objects or you wan‘t to customize behavior in specific situations. And don‘t use__just_to_underlines, unless you really know what you‘re doing!

python中的底線

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.