Various Python decorators and Python decorators
The Python decorator consists of two parts: one is the definition of the decorator itself, and the other is the definition of the decorator object.
I. Functional decorators: The decorators are a
Python adorner, divided into two parts, one is the definition of the adorner itself, one is the definition of the adorner object.
A functional decorator: the adorner itself is a function.
1. Decorative function: The object being decorated is a
A detailed explanation of various decorators in Python and a detailed explanation of Python decorators
The Python decorator consists of two parts: one is the definition of the decorator itself, and the other is the definition of the decorator object.
The definition of descriptor descriptors in Python: Typically, we can assume that "a property of an object is bound (__get__,__set__,__delete__Any one of these three methods ", then we call this property" descriptor "class Foo(object): def
(related to content: Object-oriented, class-based inheritance) defining classes and creating instancesIn Python, classes are defined by the class keyword. For example, define a person class as follows:class Person (object): PassAccording to
__str__ and __repr__If you want to turn an instance of a class into str, you need to implement a special method __str__ ():class Person (object): __init__(self, Name, gender): self.name = name Self.gender = Gender def
This article mainly introduces the summary of special methods for implementing custom classes in python, this article describes special methods such as _ str _, _ iter _, _ getitem _, _ getattr _, and _ call, for more information, see variables or
It __slots__ __xxx__ is important to note that these are similar to variables or function names, which have special uses in Python.__slots__We already know how to use it, and __len__() we know it so that we can get class to len() function.In
It's a special use in Python to see a variable or function name like __slots__ such as __xxx__.
__slots__ we already know how to use it, the __len__ () method is also known to allow class to function in the Len () function.
In addition, there are
Python bit operation:Bitwise AND &, Bitwise OR |Don't realizeBitwise XOR or ^num ^ num = 0Shift left num Shift Right >>num >> 2 = = Num/2**2Take counter ~~num = =-(num + 1)1. Single numberGiven an array of integers, every element appears twice
(Content: Custom Class)__str__ and __repr__If you want to turn an instance of a class into str, you need to implement a special Method __str__ ():class Person (object): def __init__ (self, Name, gender): self.name = name Self.gender
1. Special MethodsDefined in class There is no need to call directly, some Python functions or operators will automatically call the corresponding special Method. If the person class is defined and the instance of the person class is printed using
It __slots__ __xxx__ is important to note that these are similar to variables or function names, which have special uses in Python.__slots__We already know how to use it, and __len__() we know it so that we can get class to len() function.In
Day7 object-oriented -- Reflection, day7 object-oriented --
Reflection
You can use the following four methods to map strings or modify the status, attributes, and methods when running a program:
1. getattr (object, name [, default])-> value
Get a
Reflection operationsWhat is the reflection operator, in fact, is reversed two objects, the following first look at the implementation of a common runner:classFoo (object):def __init__(self, x): self.x=xdef __add__(self, Other):return 'foo:%s
It __slots__ __xxx__ is important to note that these are similar to variables or function names, which have special uses in Python.__slots__We already know how to use it, and __len__() we know it so that we can get class to len() function.In
Python built-in descriptor python has some built-in descriptor objects, such as PRoperty, staticmethod, and classmethod. python implementation is as follows:
Python built-in descriptor
Python has some built-in descriptor objects, such as PRoperty,
Members of the classThe members of a class can be divided into three main categories: fields, methods, and propertiesIn all members, only the contents of the normal field are saved in the object, that is: how many objects are created based on this
Http://www.myhack58.com/Article/68/2014/48183.htmBody:The usage of __new__ ():__new__ () is a newly emerging method in a new class that, before the construction method constructs an instance, can understand that the construction method that exists
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.