Properties of the Python function __ function

Source: Internet
Author: User
Looking at learn Python recently, see the effect of using function properties to accomplish nonlocal.
def test (start):
  def Nest (label):
  print (label,nest.state)
  nest.state+=1
  Nest.state=start
  Return Nest

Personally think this is typographical error. Should be as follows:

def test (start):
  def Nest (label):
    print (label,nest.state)
    nest.state+=1
  Nest.state=start
  Return Nest
But that's not what this article wants to record. Actually want to write down is a point:

def test ():

... content ...

for function test (), test.value= ... is feasible, but test (). Value= ... is not feasible.

A=test

B=test

A.value=1

b.value=1!

That is, the attribute is shared in the local space, and if you want to implement different calls to get different property values, you need to initialize them by nesting the first function.
But one question has not been tested: After modifying the properties of a function, the property is still available the next time the function is used independently. Although in and not actually does not affect the function use, but this should design the function the mechanism.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.