Object-oriented concepts are classes (class) and instances (Instance), and it is important to keep in mind that classes are abstract templates, such as student classes, and instances are specific "objects" that are created from classes, each with the same method, but the data may be different for each object.Still taki
Python implements the programming of object-oriented (object-oriented programming, or OOP) by using classes (Class) and objects (object). The main purpose of object-oriented programming is to improve the reuse of the program, which is similar to the purpose of the function.One: functionIf you need to reuse code in your program,---------define the function. def function name (parameter)://todoSuch as:Output:
class. Data in ClassclassTurtle:def __init__(self,x): Self.num=xclassFISHL:def __init__(self,x): Self.num=xclassPool:def __init(self,x,y): Self.turtle=Turtle (x) #把T中数据调入self. Turtle, Self.turtle.num self.fish can be used=Fish (y) #实例. Transfer into class. Data in ClassdefPrint_num (self):Print("turtle%d, small fish%d"% (Self.turtle.num,self.fish.num))Mix-inReproducedclassWeapon:defPrick (self, obj):#This is the device's active skill to kill each other .Obj.life_value-= 500#Assuming that the at
variable names that are defined in Python) here, the private variables are emphasized, and Python's default member functions and member variables are public, and there are no modifiers such as public,private in other similar languages. However, you can add two underscore "_" to the variable before In this case, the function or variable becomes private. This is Python's private variable rolling (this translation is very awkward), English is (private n
after the execution:>>> A.kick () I'm the ball A, why are you kicking me? >>> B.kick () I'm ball B, why are you kicking me? >>> C.kick () I'm ball C, why are you kicking me? 1.2 The Magic method in Python:Python's objects are inherently magical, and they are all about Python for the object, and they are a special way to add magic to your class, and if your object implements one of these methods, the method is called by
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 many special-purpose functions in Python's class that can help us customize our classes.
__str__
Let's first define a student class and print an instance:
for I in list, and then, the URL is passed through the view parameters, and then through the objects.get to get the article content, through another view and template display. It should be all right. These example codes are available in several tutorials. It's like Limodou's tutorial. Some old.inquiry:Then where do I go? What are the methods available for objects?Chase Answer:The question is well asked. Usually python has introspection. Directly with
, such as dictionaries. Represents the deletion of data**__new__**There are two ways to create a class:1. Common methodsClass Foo (object):def func (self):Print (' Hello world! ')2. Special methodsdef func (self):Print (' Hello world! ')Foo = type (' foo ', (object), {' Func ': func})# type first parameter: class name# type second argument: base class for the current class# type Third parameter: A member of a class**__new__**New is responsible for the opening of the memory space when the functio
We often have this kind of requirement. We want to see how many data sets and how many element classes are in the database. If we use the same number, we will find it troublesome, you can use a small Python script to complete such a task. You can complete the task in the desktop integration window in a separate Python environment, but there is no big difference,
determined before it is compiled, and the object pointed to by the reference can be dynamically bound during run timeFirst, define the class:class Dog (object): #定义 Print ("Hello class"= Dog () #实例化Second, the use of the class1. Attribute is also called member variable; method (dynamic property)classDog (object):def __init__(self, name): Self.name=name self.__heart='Normal' #Private Properties defSayhi (self):Print("Hi", Self.name)defGet_heart (self):#defining a private property access
First, review:1, the process: The core is the process, the process is the steps to solve the problem, that is, what to do first, what to do, based on process-oriented design procedures, like in the design of a pipeline, is a mechanical thinking method.Pros: Complex questions to simplifyCons: Poor scalability (holding a body)Application scenario: Linux kernel, httpd,git2, Object-oriented programming: The core is the object, to understand the object should regard themselves as God, in the eyes of
object. Self.name =name Self.age=Age Self.eye= EyedefEat (self):#print (self) Print('It's time to eat, drink, and do not forget your heart') defWork (self):Print('human beings all need work ... ') obj= Person ('Colin', 27,'Small Eyes')#This process is an instantiation process that instantiates an object (he instantiates an object space in memory). Print(obj)Print(Obj.name)There are three stages within the instantiation process:1, opens up an object space in memory2, automatically exec
precede its name with a double underline.class Test: def __fun1 (self): xxxx def fun2 (self): self . __fun1 ()As shown in the example above, __FUN1 is inaccessible from outside, but can be used inside the class. But this is not really inaccessible, because the name with the double underscore is in the form of _test_fun1. So it can be accessed through the TEST._TEST_FUN1 () Form.Previously underlined names are not imported by an import statement with an asterisk (from module
Python's abstract base class is similar to the concept of interfaces in object-oriented languages such as Java and C + +. Abstract base classes provide a way to require subclasses to implement a specified protocol, and an exception is thrown when an abstract base class requires that the specified method be implemented and the subclass is not implemented, when an attempt is made to create a subclass or execute a subclass code. Here's a brief descriptio
This article mainly introduces the differences between super () and _ init _ () in the python class. This article is very detailed and has some reference value, you can refer to the functions implemented by super () and _ init _ () when inheriting a ticket.
class Base(object):def __init__(self):print 'Base create'class childA(Base):def __init__(self):print 'creat A ',Base.__init__(self)class childB(Base):def __init__(self):print 'creat B ',super(chi
According to the "Principle of Everything" in Python, all created objects are the result of a known class instantiation, and which class is instantiated by? First look at the following piece of codeClass Foo (object): passobj = Foo () print type (obj) print type (Foo) result is: As you can see: obj is instantiated by foo, and Foo is created by the type classSo foo can do this, look at the following:def func: print "charles" Foo = Type (' Foo ',
We are familiar with the basic concepts of objects and classes. We will further expand so that we can actually use objects and classes.
Calling additional information for the class
As mentioned in the previous note, when defining a method, you must have the self parameter. This parameter represents an object. Object has all the properties of a class, we can invoke the Class property through self.
The code
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.