#-*-Coding:utf-8-*-# python:2.x__author__ = ' Administrator '#元编程#new-style class provides the ability to modify the definition of classes and objects at run time through 2 special methods (__NEW__ and __metaclass__)#__new__方法#它是一上元构建程序, it is called every time an object is instantiated by the factor class#例如:Class A (
.
When a reference is passed to a function, the function automatically copies a reference, and the reference in the function does not have a semi-gross relationship with the outside reference. So the first example is that the function refers to an immutable object number, and when the function returns, the outside reference does not feel half-hairy. And the second example is different, The reference in
superclass inherited by the subclass until it finds the object that attribute first appears.Super class, subclass. Subclasses inherit the superclass, or they can redefine the superclass's variable names at lower positions in the tree, overriding the behavior of the superclass definition (methods)class method Invocation, which is the function property attached to the class.Writing a
Note: The Python2.7 is used.
One, instance method
An instance method is a method that an instance of a class can use. As follows:
Copy Code code as follows:
Class Foo:
def __init__ (self, name):
Self.name = Name
def hi (self):
Print Self.name
if __name__ = = ' __main__ ':foo01 = Foo (' Letian ')Foo01.hi ()Print type (Foo)Print type (FOO01)Print ID (foo01)Print ID (Foo)
The
Python LearningOn this page ... (hide)
1.? Basic Installation
2.? Python documentation
2.1. Recommended Resources Site
2.2? Other references
2.3? code example
3.? Common tools
3.1? Python IDE
3.2? Built-in class
Python dynamic link library,You can change it when you need it, as long as you don't change the interface. The disadvantage is that C + + program once compiled, and then change it is not so convenient.(1) Python script: pytest.py#test functiondef Add (b): print "In Python function add" print "a =" + str (a) print "b =" + str (b) pri NT "ret =" + str
can force some of the attributes that we think must be bound to be filled in when the instance is created. By defining a special __init__ method, when creating an instance, bind the attributes such as Name,score: Class Ren (): def __init__ (self,name,sex): Self.name = Name self.sex = Sex def hello (self): print (' Hello {0} '. Format (self.name)) test = ren (' yangling ', ' M ') Test.hello ( The results are printed as:Explain: 1. When passing para
= FirstClass () # create a class instance x. display () # method call >>> 30 dir (FirstClass) >>> ['_ doc _', '_ module _', 'display ', 'Spam']
The class statement is similar to def, and is executable code. The class will not be created until the class statement is run.
In a
generic programming. Classes are defined based on placeholders or parameters at the time of the class template declaration, rather than on the actual type of property, method parameter, method return value. Make new classes/methods/properties available by replacing placeholders or parameters with actual values. Because Python is a dynamic language, there are no scenarios for generics in
answer. Write a simple version of your own understanding below.First, the class is also an object, which can:1) You can assign it to a variable2) You can copy it3) You can add properties to it4) You can pass it as a function parameterClass is also created by the function type (), type is the class class, the father of the cl
): "" defines a class method with at least one CLS parameter "" " print ' class method ' @staticmethod def static_func (): "" " defines a static method, no default parameter" "" print ' static method ' # call normal method F = Foo () F.ord_func () # Call the class method Foo.class_func () # Call the static method Foo.static_func
): attr = 2...>>> class D (b,c): Pass...>>> x = D ()>>> x.attr23. The new class adds __slots__ built-in properties that can lock the type of instance attribute to the scope specified by the __slots__.4. The new class adds the __getattribute__ method5. The new class has a built-in __new__ method and the classic
)
The Mixin class should have a single responsibility (refer to the interface design of Java, Mixin is very similar to this, but only with implementation)
According to the above standard, only multi-inheritance in the Mixin form is used, so there will be no repeated method calls resulting from diamond inheritance, and there will be no complicated search order-at this time, super is available, you don't need to look at your personal preferences. You j
Example of using urllib2 in Python to prevent 302 redirection, pythonurllib2
Note: python's urllib2 web page (urlopen) will be automatically redirected (301,302 ). However, sometimes we need to obtain the status information of the 302,301 page. You must obtain the debugging information before turning.
The following code avoids 302 redirection to a new webpage.
#!/usr/bin/
)
If according to the above criteria, only use Mixin form of multiple inheritance, then there will not be a diamond inheritance to bring a duplicate method call, there will be no complex search order-at this time, super can have no, with no need to look at a person's liking, just remember never and the class name call of the way to mix. Sometime you think that the parent name is not good, change to myparent, the result has to change each subclass t
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.