Python classes and methods of privatization

1. Create your own class

Learning object-oriented first step is to create a class. Because the class is object-oriented foundation. Python classes and other programming languages ​​(Java, C #, etc.) of similar class, but also need to use the class keyword. By following a practical example look at how Python class is created.

This creates a regular class, and use this class to create two objects, and a method in which a call.


1240

Result of the program as shown in FIG.

1240

From the above code, we can understand the following knowledge points Python class.

Python class is defined using the class keyword, class name directly behind the class keyword followed.

Class is a block of code, so the back to the class name followed by a colon (:).

The method of the class is actually a function, defined by exactly the same method, but since the functions defined within the class, so in order to distinguish the function defined in the interior of the class called method.

We can see that the first parameter is the self of each method, in fact, this is a must. This is not necessarily called self parameter name (abc can call or any other name), but any of the method must specify at least one self argument, if the method contains a number of parameters, the first parameter as a self parameter. When calling the method, the values ​​of the parameters do not need to pass, the system will be the object method belongs pass this parameter. This method may be utilized within the parameters of the calling object resource itself, such as properties, methods, and the like.

variable name added by the self parameter is a property of the Person class can be accessed externally. The present embodiment sets the value of the name attribute person2 object, exactly the same method call person2.setName effect.

Creating an object using the class way calling function the same way. In the Python language, like Java does not need to use the new keyword to create an object, just use the class name with constructor (in later chapters will detail) parameter value.

Calling the object method, there are two ways, one method is to call directly through the object variable, the other is the method by calling the class object and the corresponding first parameter passing method. Person.greet used in this example (person2) to invoke a greet method person2 object.

如果使用集成开发环境,如PyDev、PyCharm,那么代码编辑器也会对面向对象有很好的支持,例如,当在对象变量后输入一个点(.)后,IDE会为我们列出该对象中所有可以调用的资源,包括方法和属性,如下图所示。

1240

2.方法和私有化

Python类默认情况下,所有的方法都可以被外部访问。不过像很多其他编程语言,如Java、C#等,都提供了private关键字将方法私有化,也就是说只有类的内部方法才能访问私有化的方法,通过正常的方式是无法访问对象的私有化方法的(除非使用反射技术,这就另当别论了)。不过在Python类中并没有提供private或类似的关键字将方法私有化,但可以曲线救国。

在Python类的方法名前面加双下划线(__)可以让该方法在外部不可访问。


1240

如果执行上面的代码,会抛出如下图所示的异常信息,原因是调用了私有化方法method2。

1240

其实“method2”方法也不是绝对不可访问。Python编译器在编译Python源代码时并没有将“method2”方法真正私有化,而是一旦遇到方法名以双下划线(__)开头的方法,就会将方法名改成“ClassNamemethodName”的形式。其中ClassName表示该方法所在的类名,“methodName”表示方法名。ClassName前面要加上但单下划线()前缀。

For the above code, Python compiler will " method2" method was renamed "_Personmethod2", so the external call the class "method2" method throws an exception. The reason was not thrown "method2" method to be privatized, but Python compiler to "method2" The name was changed to "_Person method2" a. When we understand the principles behind these, you can call "_Person to perform" method2 "method method2" method.


1240

This creates a regular meeting MyClass class, and defines two public methods (getName and the setName) and a proprietary method ( outname). And then creates an instance of the class MyClass, and call these methods. To demonstrate Python compiler when compiling class MyClass tampered, the present embodiment also uses the inspect module getmembers function to get all members of the class MyClass method, and outputs the name of the method. Obviously, " outname" was changed to "_MyClass__outName".


1240

Result of the program as shown in FIG.

1240

As can be seen from the name MyClass class method getmembers functions listed, "_ MyClass outName" is bound to " the outName" method, we can "_MyClass outName" seen " outName" is an alias, once for a methods surnamed, then the original name outside the class is not available. Name Alias same method getName and setName methods MyClass class and original method, it can be outside the direct call getName and setName methods.


Guess you like

Origin blog.51cto.com/14246112/2404904