The object-oriented _python of the Python introductory article

Source: Internet
Author: User
Tags class definition documentation garbage collection stdin vars in python

The relationship between object-oriented design and object-oriented programming

Object-oriented design (OOD) does not specifically require object-oriented programming languages. In fact, OOD can be implemented by a purely structured language, such as C, but if you want to construct data types that have the nature and characteristics of objects, you need to do more work on the program. When Oo features are built into a language, OO programming is more efficient and easy to develop. On the other hand, an object-oriented language does not necessarily force you to write OO-related programs. For example, C + + can be thought of as "better C"; Java, in turn, requires that everything be class, and that a source file corresponds to a class definition. In Python, however, classes and OOP are not necessary for everyday programming. Although it was designed from the outset object-oriented and structured to support OOP, Python does not qualify or require you to write OO code in your application. OOP is a powerful tool that can be arbitrarily controlled whether you are ready to enter, learn, transition, or turn to OOP. One of the most important reasons to consider working with OOD is that it directly provides a way to model and solve real-world problems and situations.

Class

A class is a data structure that we can use to define objects, which combine data values with behavioral characteristics. A class is a real-world abstract entity that appears in a programmatic fashion. Instances are the materialization of these objects. As an analogy, a class is a blueprint or a model used to produce real objects (instances). A class can also derive similar but differentiated subclasses. Many of these features are applied to the concept of classes in programming. In Python, a class declaration is similar to a function declaration, with a corresponding keyword in the first line, followed by a body of code as its definition, as follows:

Copy Code code as follows:

def functionname (args):
' function documentation string ' #函数文档字符串
Function_suite #函数体
Class ClassName (object):
' Class documentation string ' #类文档字符串
Class_suite #类体

Both allow you to create functions in their declarations, closures or internal functions (that is, functions within functions), as well as methods defined in the class. The biggest difference is that you run the function, and the class creates an object. Class is like a Python container type. Although classes are objects (all objects in Python), they are not implementations of objects when they are being defined.

Creating a Class

The Python class is created using the class keyword. A simple class declaration can be a keyword followed by the class name:

Copy Code code as follows:

Class ClassName (Bases):
' Class documentation string ' # ' Classes document Strings '
Class_suite #类体

A base class is a collection of one or more parent classes used for inheritance, and the class body consists of all declaration statements, class member definitions, data properties, and functions. Classes are typically defined at the top level of a module so that class instances can be defined in the class

is created anywhere in the source code file.

Declaration and definition
For Python functions, declarations are no different from defining classes because they are simultaneous, and the definition (class body) is immediately following the declaration (the header line with the Class keyword) and the optional document string. At the same time, all methods must be defined at the same time. If you are familiar with OOP, note that Python does not support pure virtual functions (like C + +) or abstract methods (such as in JAVA), which forces programmers to define methods in subclasses. As an alternative, you can simply throw a Notimplementederror exception in the base class method, which results in a similar effect.

Class properties

A property is a data or function element that belongs to another object, and can be accessed through the familiar Period attribute identification method. Some Python types such as complex numbers have data attributes (real and imaginary), while others, like lists and dictionaries, have methods (function properties).

One interesting thing about attributes is that when you're accessing an attribute, it's also an object that has its own properties and can be accessed, which leads to a chain of attributes, such as mything,subthing,subsubthing. Wait a minute.

Data properties of a class

The Data property is only a variable of the defined class. They can be used, like any other variable, after the class is created, and are either updated by methods in the class or elsewhere in the main program.
This attribute is familiar to OO programmers, i.e. static variables, or static data. They represent that the data is bound to the class object to which they belong, and does not depend on any class instance. If you are a Java or C + + programmer, this type of data is equivalent to adding a static keyword before a variable declaration. Static members are typically used only to track values associated with a class.

Look at the example below, using Class data Properties (foo):

Copy Code code as follows:

>>> Class C (object):
foo = 100
>>> Print C.foo
100
>>> c.foo+=1
>>> C.foo
101

Method

Copy Code code as follows:

>>> class MyClass (object):
def mynoactionmethod (self):
Pass
>>> MC = MyClass ()
>>> Mc.mynoactionmethod ()

Any call to Mynoactionmethod itself, like a function, will fail:

Copy Code code as follows:

>>> Mynoactionmethod () traceback (innermost last):
File "<stdin>", line 1, in?
Mynoactionmethod () Nameerror:mynoactionmethod

Even calling this method by a class object fails.

Copy Code code as follows:

>>> Myclass.mynoactionmethod () traceback (innermost last):
File "<stdin>", line 1, in?
Myclass.mynoactionmethod ()
Typeerror:unbound method must is called with class
Instance 1st argument

Bindings (binding and unbound methods)
To keep in line with OOP conventions, Python is strict in that there are no instances, and methods cannot be invoked. This restriction is the binding concept described by Python (binding), where the method must be bound (to an instance) to be invoked directly. The unbound method may be invoked, but the instance object must be explicitly given in order to ensure that the call succeeds. However, regardless of binding, the method is the intrinsic property of the class in which it resides, even though they are almost always invoked through an instance.

Determining the properties of a class

There are two ways to know what properties a class has. The simplest is to use the DIR () built-in function. Another is by accessing the dictionary property __dict__ of a class, which is one of the special attributes that all classes have.

Take a look at the following example:

Copy Code code as follows:

>>> class MyClass (object):
' MyClass class definition ' #类定义
Myversion = ' 1.1 ' #静态数据
def showvesion (self): #方法
Print Myclass.myversion

>>> dir (MyClass)

Run Result:

Copy Code code as follows:

[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __ module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __ Subclasshook__ ', ' __weakref__ ', ' myversion ', ' showvesion '

Use:

Copy Code code as follows:

>>> myclass.__dict__
Dict_proxy ({' __module__ ': ' __main__ ', ' showvesion ': <function showvesion at 0x0134c9b0>, ' __dict__ ': < Attribute ' __dict__ ' of ' MyClass ' objects>, ' myversion ': ' 1.1 ', ' __weakref__ ': <attribute ' __weakref__ ' of ' MyClass ' ' Objects>, ' __doc__ ': ' MyClass class definition '}

As you can see from above, dir () returns only a list of names for the attributes of the object, and __dict__ returns a dictionary whose key (keys) is the property name, and the key value (values) is the data value of the corresponding Property object.
The results also show two familiar properties, Showmyversion and Myversion, and some new properties in the MyClass class. These attributes, __doc__ and __module__, are special class attributes for all classes (plus __dict__). The built-in VARs () function takes the class object as a parameter and returns the contents of the __dict__ property of the class.

Special class Properties

For any class C, the table shows all the special properties of Class C:
Name of c.__name__ class C (String)
Document string for c.__doc__ class C
Tuple of all the parent classes of C.__BASES__ Class C
Properties of c.__dict__ Class C
C.__MODULE__ Class C Definition of the module (new version 1.5)
c.__class__ instance C corresponding Class (in new class only)

Copy Code code as follows:

>>> myclass.__name__
' MyClass '
>>> myclass.__doc__
' MyClass class definition '
>>> myclass.__bases__
(<type ' object ';)
>>> Print myclass.__dict__
{' __module__ ': ' __main__ ', ' showvesion ': <function showvesion at 0x0134c9b0>, ' __dict__ ': <attribute ' __dict__ ' MyClass ' objects>, ' myversion ': ' 1.1 ', ' __weakref__ ': <attribute ' __weakref__ ' of ' MyClass ' objects>, ' __do c__ ': ' MyClass class definition '}
>>> myclass.__module__
' __main__ '
>>> myclass.__class__
<type ' type ' >

Instance

If the class is a data structure definition type, then the instance declares a variable of this type. An instance is an object that is primarily used at runtime, and the class is instantiated for instance, and the type of the instance is the instantiated class.

Initialization: Creating an instance by calling a class object

The Python approach is much simpler. Once you have defined a class, it is easier to create an instance than to call a function------effortless. An instantiated implementation, you can use the function operator, as shown here:

>>> class MyClass (object): # define class definition classes
Pass
>>> MC = MyClass () # Instantiate class initialization classes
__init__ () constructor method

When a class is invoked, the first step in instantiating is to create an instance object. Once the object is created, Python checks to see if the __init__ () method is implemented. By default, no special action is applied to an instance without defining (or overriding) a special method, __init__ (). Any specific action required requires the programmer to implement __init__ (), overriding its default behavior.

If __init__ () is not implemented, its object is returned and the instantiation process is complete.

If __INIT__ () is implemented, it is invoked, and the instance object is passed in as the first argument (self), like a standard method call. When the class is invoked, any arguments that are passed are given to __init__ (). In practice, you can imagine that the call to create an instance is a call to a constructor.

__new__ () constructor method

The __new__ () method is more like a real constructor than __init__ (). You need a way to instantiate immutable objects, such as derived strings, numbers, and so on. In this case, the interpreter invokes the __new__ () method of the class, a static method, and the incoming arguments are generated when the class instantiation operation. __NEW__ () invokes the __new__ () of the parent class to create the object (up proxy). __NEW__ () must return a valid instance.

__del__ () Deconstruction method

Similarly, there is a corresponding special destructor (destructor) method named __del__ (). However, since Python has a garbage collection mechanism (counting by reference), this function does not execute until all references to the instance object are cleared. A destructor in Python is a way to provide special processing functionality before an instance is released, which is usually not implemented because instances are rarely explicitly released.

Note: Python does not provide any internal mechanism to track how many instances of a class have been created, or what these instances are. If you need these features, you can explicitly add some code to the class definition or __init__ () and __del__ (). The best way to do this is to use a static member to record the number of instances. It is dangerous to keep track of instance objects by saving their references, because you have to manage them properly, or your references may not be able to be released (because there are other references)! Look at one of the following examples:

Copy Code code as follows:

>>> class INSTCT (object):
Count = 0
def __init__ (self):
Instct.count + 1
def __del__ (self):
Instct.count-= 1
def howmany (self):
Return Instct.count

>>> a = INSTCT ()
>>> B = INSTCT ()
>>> B.howmany ()
2
>>> A.howmany ()
2
>>> del b
>>> A.howmany ()
1
>>> del A
>>> Instct.count
0

Instance Properties

Setting the properties of an instance can take place at any time after the instance is created, or in code that can access the instance. Constructor __init () is one of the key points for setting these properties

Being able to create instance properties at run time is one of the great features of the Python class, and Python is not only a dynamic type, but also allows the dynamic creation of these object properties at run time. This trait is not to be loved.
Hand. Of course, when you create such a property, you must be cautious. A flaw is that a property is created in a conditional statement, and if the conditional statement block is not executed, the property does not exist, and you are trying to access the properties in the following code, and there will be an error.

Default parameter provides default instance installation
In practical applications, __init__ () with default parameters provides an efficient way to initialize an instance. In many cases, the default value represents the most common case of setting instance properties, and if a default value is provided, it is not necessary to explicitly pass the value to the constructor.

Copy Code code as follows:

>> class Hotelroomcalc (object):
' hotel room rate calculate '
def __init__ (self, rt, sales = 0.085, RM = 0.1):
"' Hotelroomcalc default arguments:
Sales tax = = 8.5% and room tax = = 10% ' "
Self.salestax = Sales
Self.roomtax = RM
Self.roomrate = RT
def calctotal (self, days = 1):
' Calculate Total:default to the daily rate '
Daily = Round ((self.roomrate * (1+self.roomtax + self.salestax)), 2
return float (days) * Daily

>>> SFO = hotelroomcalc (299)
>>> Sfo.calctotal ()
4960.41
>>> Sfo.calctotal (2)
9920.82
>>> sea = Hotelroomcalc (189, 0.086, 0.085)
>>> Sea.calctotal ()
3098.47
>>> Sea.calctotal (4)
12393.88

All the flexibility of a function, such as the default parameter, can also be applied to a method. Variable-length parameters are also a good feature when instantiated

__init__ () should return None
Invoking a class object with a function operator creates a class instance, which means that the object returned by such a call procedure is an instance, as shown in the following example:

Copy Code code as follows:

>>> class MyClass (object):
Pass

>>> MC = MyClass ()
>>> MC
<__main__. MyClass Object at 0x0134e610>

If a constructor is defined, it should not return any objects because the instance object is automatically returned after the instantiation call. Accordingly, __init__ () should not return any objects (None); otherwise, conflicts may occur because only instances can be returned. Trying to return any other object that is not None will result in a TypeError exception:

Copy Code code as follows:

>>> class MyClass:
def __init__ (self):
print ' initialized '
Return 1

>>> MC = MyClass ()
Initialized
Traceback (most recent call last):
File "<pyshell#86>", line 1, in <module>
MC = MyClass ()
TypeError: __init__ () should return None

View instance Properties

The built-in function dir () can display the class properties, and you can also print all instance properties:

Copy Code code as follows:

>>> C = C ()
>>> C.foo = ' he '
>>> c.bar = ' Isa '
>>> dir (c)
[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __ module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __ Subclasshook__ ', ' __weakref__ ', ' Bar ', ' foo ']

Like a class, an instance has a __dict__ special attribute (which can call VARs () and pass in an instance), which is a dictionary of instance properties:

Copy Code code as follows:

>>> c.__dict__
{' foo ': ' He ', ' Bar ': ' Isa '}

Special Instance Properties

Instance has only two special properties. For any object I:
Class of i.__class__ instantiation I
Properties of i.__dict__ I

Copy Code code as follows:

>>> class C (object):
Pass
>>> C = C ()
>>> dir (c)
[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ', ' __getattribute__ ', ' __hash__ ', ' __init__ ', ' __ module__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __ Subclasshook__ ', ' __weakref__ ']
>>> c.__dict__
{}
>>> c.__class__
<class ' __main__. C ' >>>> #可以看到, C has no attributes
>>> C.foo = 1
>>> C.bar = ' ewe '
>>>%d can of%s please '% (C.foo, C.bar)
' 1 can of ewe please '
>>> c.__dict__
{' Foo ': 1, ' bar ': ' Ewe '}

Built-in Type properties

Built-in types are also classes, and the built-in type can also use Dir (), which, like any other object, can get a list containing its property name:

Copy Code code as follows:

>>> x = 2 + 2.4j
>>> x.__class__
<type ' Complex ' >
>>> dir (x)
[' __abs__ ', ' __add__ ', ' __class__ ', ' __coerce__ ', ' __delattr__ ', ' __div__ ', ' __divmod__ ', ' __doc__ ', ' __eq__ ', ' __float __ ', ' __floordiv__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __getnewargs__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' _ _int__ ', ' __le__ ', ' __long__ ', ' __lt__ ', ' __mod__ ', ' __mul__ ', ' __ne__ ', ' __neg__ ', ' __new__ ', ' __nonzero__ ', ' __pos__ ', ' __pow__ ', ' __radd__ ', ' __rdiv__ ', ' __rdivmod__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __rfloordiv__ ', ' __rmod_ _ ', ' __rmul__ ', ' __rpow__ ', ' __rsub__ ', ' __rtruediv__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __sub__ ', ' __ Subclasshook__ ', ' __truediv__ ', ' conjugate ', ' imag ', ' real '

Trying to access __dict__ will fail because this property does not exist in the built-in type

Instance Properties vs Class properties

Class properties are only data values that are related to the class, and are not the same as instance properties. These values are referenced like static members, and their values remain unchanged even when the class is invoked in multiple instantiations. In any case, static members do not change their values because of an instance, unless their values are explicitly changed in the instance. Both the class and the instance are namespace names. The class is the namespace of the Class property, and the instance is the instance property.
There are some aspects to note about class and instance properties. Classes can be used to access class properties, and you can use instances to access them if the instance does not have an attribute of the same name.

Accessing class Properties
Class properties can be accessed through classes or instances. In the following example, Class C takes a version property when it is created, so it is natural to access it through a class object, for example, c.version

Copy Code code as follows:

>>> class C (object):
Version = 2

>>> C = C ()
>>> c.version
2
>>> c.version
2
>>> C.version + + 2
>>> c.version
4
>>> c.version
4

Accessing class properties from an instance requires caution

As with a common Python variable, any assignment to an instance property creates an instance property (if it does not exist) and assigns a value to it. Side effects occur if a property with the same name exists in the Class property.

Copy Code code as follows:

>>> class Foo (object):
x = 1

>>> foo =foo ()
>>> foo.x
1
>>> foo.x = 2
>>> foo.x
1

After using Del

Copy Code code as follows:

>>> del foo.x
>>> foo.x
1

Static members, as their names say, allow the entire instance (and its attributes) to progress, and it does not matter (and therefore is independent of the instance). Also, when an instance is created after the class attribute is modified, the updated value takes effect. The modification of a class property affects all instances:

Copy Code code as follows:

>>> class C (object):
Spam = 11

>>> C1 = C ()
>>> C1.spam
11
>>> C.spam + + 2
>>> C.spam
13
>>> C1.spam
13
>>> C2 = C ()
>>> C2.spam
13
>>> del C1
>>> C.spam + + 3
>>> C2.spam
16

As you can see above, it is dangerous to try to modify class properties using instance properties. The reason is that instances have their own set of properties, and there is no explicit way in Python to indicate that you want to modify class attributes with the same name, and that you need to use the class name instead of the instance name to modify the class attribute.

Static Methods and Class methods

Static methods and class methods are introduced in Python2.2. It can be used in both classic and new (New-style) classes. A pair of built-in functions is introduced to declare "tag", "Cast" or "convert" as one of the two types of methods for a method that is part of a class definition.

Now let's take a look at some examples of creating static methods and class methods in classic classes:

Copy Code code as follows:

>>> class Teststaticmethod:
def foo ():
print ' calling static method foo () '
foo = Staticmethod (foo)

>>> class Testclassmethod:
def foo (CLS):
print ' Calling class Method Foo () '
print ' foo () is part of class: ', cls.__name__
foo = Classmethod (foo)

The corresponding BUILTIN functions are converted to their corresponding types, and the same variable names are assigned to them. If these two functions are not invoked, both will generate an error in the Python compiler, displaying a general method declaration that requires self.

Copy Code code as follows:

>>> TSM = Teststaticmethod ()
>>> Teststaticmethod.foo ()
Calling static method Foo ()
>>> Tsm.foo ()
Calling static method Foo ()
>>> TCM = Testclassmethod ()
>>> Testclassmethod.foo ()
Calling class Method Foo ()
Foo () is part of Class:testclassmethod
>>> Tcm.foo ()
Calling class Method Foo ()
Foo () is part of Class:testclassmethod

Use function Modifiers:

New features added to the Python2.4. You can use it to apply a function to another function object, and the new function object is still bound to the original variable. We just need it to sort out the grammar. By using decorators, we can avoid the same redistribution as above:

Copy Code code as follows:

>>> class Teststaticmethod:
@staticmethod
def foo ():
print ' calling static method foo () '

>>> class Testclassmethod:
@classmethod
def foo (CLS):
print ' Calling class Method Foo () '
print ' foo () is part of class: ', cls.__name__

Related Article

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.