How to Implement Python configuration?

Source: Internet
Author: User

In a text area in Python configuration, a namespace can be directly accessed. "direct access" refers to finding objects in the namespace directly using a name without modification, you can directly learn Python without starting with C.

For example, all method names start with an uppercase letter, and all data attribute names start with a unique string or only an underline), or method names use verbs and data names use nouns. Data attributes can be referenced by methods or by common users "customers. In other words, classes cannot be used to construct abstract data types. Actually.

There is no way to force data hiding in Python-these are based on conventions. On the other hand, the implementation of Python is written in C. It can completely hide the implementation details and control object access if necessary. The Python extension module written in C also has the same features ).

Customers must be careful when using data properties. Customers may change the data properties of class objects at will to undermine the consistency of class data maintained by class methods. Note: To avoid name conflicts, you can add new data attributes to the instance object without affecting the validity of the method. Here, effective naming conventions can save a lot of trouble.

There is no abbreviated method to access the data attributes or other methods of the object from the method. I think this actually increases the readability of the program: local variables and instance variables are not obfuscated in the method definition. In habits, the first independent variable of the method is self. This is just a habit: Name self has no special meaning in Python configuration. However.

Because users use this Convention, violating this Convention may make it difficult for other Python configurations to read your program. You can imagine that some browser programs will depend on this Convention ). Any function object as a class property defines a method for the class instance. The function definition does not necessarily have to be within the class definition: you only need to assign a function object to a local variable in the class. For example:

 
 
  1. >>> class Complex:  
  2. ...     def __init__(self, realpart, imagpart):  
  3. ...         self.r = realpart 
  4. ...         self.i = imagpart 
  5. ...   
  6. >>> x = Complex(3.0,-4.5)  
  7. >>> x.r, x.i  
  8. (3.0, -4.5)  

At present, f, g, and h are Class C attributes and point to function objects, so they are all the methods of the C instance-where h and g are completely equivalent. Note that we should avoid this usage to avoid misleading readers. Methods can reference other methods in this class by representing the self independent variable of the object to which the object belongs. For example:

 
 
  1. class VirtualAttributes:  
  2.     __vdict = None 
  3.     __vdict_name = locals().keys()[0]  
  4.        
  5.     def __init__(self):  
  6.         self.__dict__[self.__vdict_name] = {}  
  7.       
  8.     def __getattr__(self, name):  
  9.         return self.__vdict[name]  
  10.       
  11.     def __setattr__(self, name, value):  
  12.         self.__vdict[name] = value 

The global name can be referenced like a common function. The global scope of a method is a module that contains the class definition. Note that the class itself is not used as a global scope !) Although we seldom need to use global data in methods, global scopes have many valid purposes:

For example, Python configurations and modules imported to the global scope can be used by methods. Functions and methods defined in the same module can also be used by methods. Classes that contain this method are also defined in this global scope. In the next section, we will see why a method needs to reference its own class!

A Python program that requires an abstract data type as the input can often accept a class as the input, which only imitates the method of the data type to be input. For example, if you have a function that is used to format data in a file object, you can define a class with methods of read () and readline, this class can be input from a string buffer instead of input from a file. This class is used as the independent variable.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.