Paste notes outline some of the self-study process, the source text in pycharm written inside, tidy bit of a mess, to be part of supplement, writing is not necessarily 100% correct, when the full data backup.
1. The object-oriented features
# What do you use when writing object-oriented code is # relationship between the process more complex roles # such as QQ friends and strangers, Taobao sellers buyers of goods, the company's personnel management school # higher-resolution code, whether developers the caller can clearly distinguish their method # code has scalability, readability, reusability
2. With regard to class
# Relationship types and classes of # relationship types and custom classes, types and classes are a thing # type (obj) obj is an object, his type is the type of # how to create a class # Python explain class class name syntax level when the sentence is read # class is being created out of, type create a class, type (CLS) of the type = # of the type (obj) the result is that the object belongs to the class of # of the type (class) of this class is to create results yuan class, in most cases is to type unless you specify the metaclass that # class: direct loading into, not only a function call to load # static properties / fields / variables # dynamic properties / methods
3. About Objects
# Python everything is an object # basic data types are objects # how to create an object # class name () to instantiate # __new __ () to create a space object, some simple initialization # Object # can find space class by a pointer content # object itself stores only some of the properties belonging to the object # class to create an object of the process is the process of instantiation
3. The combination of inheritance and
# Combinations: What is the relationship between what # one object to another object's properties as # inheritance: What is the relationship between what # single inheritance / multiple inheritance # single inheritance: If the object subclass call a method, there is a subclass of class tone , the subclass does not find a parent, has been looking for, until then an error object # subclasses have wanted to tune parent: # . Super (sub-class, self) method name (in addition to the self parameter) # parent class name: father the class name method name (self) # method calls in any class, must differentiate themselves some of the self who is the object? # multiple inheritance: the new class inherits the default object, py3 are not inherited by the new class classic Object # classic, new-style class # new: breadth-first, c3 algorithm # MRO ways of looking at the order of succession # Super () internal func () class do not pass subclass name and Self. # classic: depth-first # no MRO # Super (subclass name , self) .func () must pass subclass name and Self # subclass and superclass: save code # abstract class and interface class # Can not be instantiated # specification subcategory which must implement a method # there are ways native implement an abstract class, but there is no way a native implementation of the interface class # the Java supports only method single inheritance of the parent class can achieve # the Java, the interfaces interface supports multiple inheritance norms, all the methods in the interface can only write Pass # that is the single abstract class inheritance, interface class is multiple inheritance, which occurred mainly in the java
4. Polymorphic type ducks
# Duck Type: # e.g. sequence index characteristic class, said classes are the types of duck # Polymorphism: polymorphism everywhere # various forms one type of a plurality of sub-classes to inherit the parent class, each sub-class is a form of the parent class # java # DEF FUNC (int arg1, arg2 str) # for java, the incoming object type requires # if you do not know what class pass, you can create a common parent, the parent passed in class. this is the java, multi-state application # Python # because all classes of objects have object classes, object classes can be said that all of the parent class, so that everywhere there is polymorphic py
5. Package
# Package # broad package: the method attribute encapsulated in a class, define a specification to describe a class of things # narrowly package: privatization can only be accessed from within the class # __ static variables, private methods, private object properties private class method private static method # memory storage class name __ __ name # Why inner class can be used in the double-underlined access? use within the class, you will know in which class # can subclass access the parent class's private variables do? No! can not be inherited
6. decorator function property
# Property decorator function of # He's built-in functions, methods will help you disguised as class attributes, characteristics! # Invoke method when no active brackets, so that the logic of the program is more reasonable # @ name of the method .setter decorator, decorative modify the property when the property will call this method are decorated decorator, in addition there is a self parameter value is modified parameter # @ name .deleter method decorator, when you want to remove the property decorated property when calls are decorated decorator method # a circle calculated example of class circle: DEF the __init__ (Self, R & lt): self.r = R & lt @Property DEF Area (Self): # this calculation method itself is a property , but this property will vary substantially with the class / object varies return 3.14 ** * self.r 2 C = Circle (. 5 ) Print (c.area)
Classmethod and decorator decorator staticmethod
# Class method decorators, but also built-in functions classmethod # do not target namespace content, and use the variables (static properties) class namespace, class methods or class attributes # class name to call, the default name of the class to do the first pass parameter # staticmethod static method decorator built-in functions # If a class method, neither the need to use self in resources, cls do not have the resources, equivalent to a general function, but based on some reason, the method still take place to class, then you need to become a static method method # you totally want to use object-oriented programming, all functions must be written class # certain function is indeed the method of this class, indeed useless this class related to the resource # such as students and administrators need to be logged, students and administrators do not want to occupy the resource class the Person: @staticmethod DEF the login (): Pass class student (the Person): Pass class Manager (the Person): pass
7. reflection
# Reflected: from a specified namespace, the data type of string variable name to obtain the value of the variable # static property class class method names reflecting static method # Object embodiment Object Properties Method # Method module module # own module reflection, for example (where incomplete complementary module must wait portion) Import SYS mymodoule = the sys.modules [ ' __main__ ' ] getattr (mymodoule, ' variable names ' ) # the hasattr / getattr / setattr / delattr # parameters # ( namespace 'variable name') # setattr (namespace, the new value of the 'variable names') # variable names you can get a string version of # get from file # interaction take: input / network transmission
8. The method of the bis
9. A destructor
10.item method
11.hash method
12. Module acquaintance