Python Knowledge points

Source: Internet
Author: User

    • Generator (generator)

      An iterator built using a generator function or builder expression. You can generate values without iterating over the collection.

      In contrast to iterators, the generator is suitable for scenarios where a large collection of data needs to be handled. Because there is no need to prepare all the elements of the entire iteration in advance. That is, the order of all elements of an object is stored in memory before the operation begins. can effectively save memory.

      However, due to the characteristics of lazy computation, the time overhead of generators is slightly higher.

    • Generator functions (generatorfunction)

      Functions with the yield keyword are called generator function (generator functions) in Python.

      The Call builder function gets the generator.

      The Python interpreter treats the function with the yield keyword as a generator. A function or subroutine can only return one time. However, a generator can pause execution and return an intermediate result.

      This is the function of the yield statement: Returns an intermediate value to the caller and pauses execution.

    • Generator Expressions (generatorexpression)

      In parentheses, the syntax is the same as the list derivation, but instead of the list, the generator is returned.

      Builder expressions can be understood as lazy versions of list derivation.

?

    • Iterative (iterable)

      Use the built-in ITER function to derive an iterator object from it.

      If an object's __iter__ method can return an iterator , this is an iterative object. Sequences are objects that can be iterated.

      In addition, the object that implements the __getitem__ method is also an iterative object. However, the subscript must be accessed starting from 0.

      An iterative object should implement __iter__ a method without implementing a __next__ method.

    • Iterators (iterator)

      __next__an object that implements a parameterless method. This method returns the next element in the series. Throws a Stopiteration exception if there are no elements.

      So iterators can only iterate once.

      In Python, iterators also implement __iter__ methods, so iterators are also 可迭代的对象 .

      Based on the original design pattern, the classic iterator returns the elements in the collection. The generator is also an iterator, but more flexible.

?

  • Decorative Device

    An adorner is essentially a Python function that allows other functions to add extra functionality without any code changes, and the return value of the adorner is also a function object.

  • Descriptor

    A descriptor is a class that implements a specific protocol, including, for this protocol, a __get__ __set__ __delete__ method.

  • Closed Package

      • Closures refer to functions that extend the scope. It contains non-global variables that are referenced in the body of the function definition, but are not defined in the definition body.
    • This is called a free variable, and the quoted free variable will exist with this function, even if it has left the environment to create it.

    • function is not anonymous, the key is that it can access the definition of non-global variables defined outside the body.
    1. An intrinsic function can use the behavior of an external function variable, which is called a closure.
    2. When a function is nested, and a child function references a variable in the parent function, the scope of the variable can be accessed to form a closure.
    3. If a child function does not have access to a variable in the parent function, there is no closure.
  • Meta-Class (metaclass)

    The instance is the class class, by default, the class in Python is an instance of the type class, for example, the type (int) results in the type class. Therefore, the type is a meta-class. The user can define the meta class by extending the type class.

  • Meta programming (metaprogramming)

    The program is written using the program runtime information to change the behavior of the program. For example, ORM might introspect the declaration of a model class, determine how to validate the fields in a database record, and convert the database type to a Python type.

  • Callback Functions (callbackfunction)

    A function that is first passed to the middle function is called a callback function.

    Cases.
    You go to a shop to buy things, just what you want to have no goods, so you left your phone in the shop clerk, after a few days the store has goods, the clerk called you, and then you received a call to the store to pick up the goods. In this example, your phone number is called a callback function, you leave the phone to the clerk called the registration callback function, the store later in the goods called triggered the callback associated event, the clerk called you called Call callback function, you go to the store to fetch goods called response callback event.

    • Start function

      The starting function refers to the caller of the intermediate function.

    • Blocking callbacks

      In a blocking callback, the call to the callback function must occur before the start function returns.

    • Deferred callback

      In a deferred callback, the call to the callback function may be after the start function returns.

  • Weak references

    A weak reference is a reference that cannot ensure that the object it references is not reclaimed by the garbage collector.

    An object may be reclaimed at any time if it is referenced only by a weak reference.

    The primary function of a weak reference is to reduce the number of unnecessary objects in memory by reducing circular references.

    Python reclaims this object when the object's reference count is 0 or if only the weak reference to the object exists

  • Monkey Patch (monkey patching)

    Used to dynamically modify existing code at run time without the need to modify the original code.

    Typically, you add features or fix bugs.

    The main uses are:

    1. Dynamically replace methods, properties, and so on at run time

      import json  import ujson def monkey_patch_json():      json.__name__ = ‘ujson‘      json.dumps = ujson.dumps      json.loads = ujson.loads  
    2. Add features that were not supported without modifying third-party code

    3. Add patches to in-memory objects at run time instead of adding them to the disk's source code

    Monkey patches in memory play a role, will not modify the source code. Therefore, it is valid only for the currently running program instance. This is considered a temporary workaround because the monkey patch destroys the encapsulation and easily causes the program to be tightly coupled to the implementation details of the patch code.

  • Duck type

    A form of polymorphism.

    An object's valid semantics are determined not by inheriting from a particular class or implementing a particular interface, but by the collection of current methods and properties .

  • Context Manager

    An __enter__ __exit__ object that implements a special method and is used in a with block.

  • Abstract base class, (abstractbaseclass, ABC)

    You cannot instantiate a class that can only be extended. Python implements the interface via ABC. In addition to inheriting ABC, a class can also register as a virtual subclass of ABC. Declares itself to implement the interface.

  • Sub-Program
  • Co-process

    Generators for concurrent programming

    Functions that can pause execution

  • Grammatical sugars (syntactical sugar)

    Also known as icing syntax, is a convenient syntax that does not affect program functionality, but can improve code readability.

  • __dict__

    __dict__is a dictionary used to store object properties whose key is the property name and the value of the property.

  • LEGB Principles

    Local (function), native
    Enclosing function local, closure
    Global (module), globally
    Builtion (Python), module

  • Delay binding
  • The difference between GET, POST, put
  • Cookie (client hold state)
  • Session (server-side hold state)

    The session mechanism may need to use a cookie mechanism to achieve the purpose of preserving the identity

Python Knowledge points

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.