Two little things about Python----Functional Programming & class instances

Source: Internet
Author: User

Functional Programming LanguagesHttp://program-think.blogspot.com/2012/02/why-choose-python-4-fp.htmlPure Function: Output completely dependent on input functions, cannot read and write external variables, no IOrelated built-in functions in Python: 1. The map (function, iterator) passes all the contents of the iterator to the function in turn, and returns an iterator that can be a list ... e.g.map (Lamda x:x*2, [1, 2, 3])= = Returns [1, 4, 9]2.filter (function, iterator) to save the function (iterator content) to true e.g in an iterator.filter (Lamda x:x% 2 = = 1, [1, 2, 3])= = Returns [1, 3]3.list comprehension e.g.[n for N in list if (n%2) = = 1]An example of a calculus:def D (f):def calc (x):dx = 0.0001return (f (x + dx)-f (x))/dxReturn Calcf = Lamda x:x**2 + 5 * x + 3to find the derivative of f: f1 = d (f)slope at a certain point: F1 (2)
instances of the classIn Python, classes also have instances. The instance of the class contains functions and class properties. With Class a://test.pyclass A:count = 0def __init__ (self)A.count + = 1def __del__ (self)A.count-= 1and there's an example of aa = a ()End I run the above statement in the Python2.7 version of IdleWhat's going to happen? To Class A, an instance is generated, in memory we call itA1UnderA1, the instance a,a the object instance pointed to is generated we call itA1Now A.count is 1, i.e.A1The count in the instance is 1 at this point, if we exit the program, a will not be immediately recycled so A.count still 1,sys.getrefcount (a) is still 2 (and 1 is incremented for incoming parameters)now open test.py run once, what will happen then? First, a new instance of a is generated and we call itA2Bar,A2The count is 0. At this timeA1Still exists now run A = A (), according toA2Generate object instances, which we callA2。A2To make an instance of the new A in memoryA2. Count = 1 At this point a points toA2, so A1 this piece of content ref count is 0 and will callA1. __del__ the question came, why at this timeA2. Count = 0 is that it?because A2. Count overrides the code a.count in the A1 instance, if A.count is changed to Self.__class__.count then __DEL__ is still modified in A1. Count does it look like super?

Two little things about Python----Functional Programming & class instances

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.