“This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

A, XML, The use of

  1. A good module for handling XML message packages is xml.etree.elementTree.
  2. Element executes the root node of the XML.
  3. Elem. Find (path): Finds the child node whose path is path under the root node.
  4. Elem.findall (path): There may be multiple children of the same node, and a list is returned.
  5. Elem.findtext (path): Gets the contents of the child nodes of the specified path. We’ll use this a lot.
  6. elem.get(key); Gets the value of the property.
  7. If not, return None
  8. Elem. Append: Add from the node.
  9. Elem. Tag: Returns the tag value, which is name.
  10. Elem. Text: Returns the content.
  11. Elem. Attrib: Returns a dictionary of attributes.
  12. SubElement: A node is generated and automatically added as a child node of the parent node.
  13. Tostring: Converts to AN XML text string. XML headers are not included. If the encoding is UTF-8 or GB2312, both generate XML headers. If it is UTF-8, no XML header is generated
  14. Fromstring: Converts a string into an ElementTree object. Same functionality as XML.
  15. elem.set(); Setting property values

Second, the time The use of

  1. The time.sleep() function has the function of sleep under C in seconds, but can accept floating point numbers. This is the millisecond.
  2. Ti = datetime.datetime.now() displays the current time, including the current microsecond. The difference between the two can indicate the time interval: microsecondLong = timeLong. Seconds * 1000000 + timeLong. The members of the difference are seconds and microseconds

Third, OO The use of

1. If you do not want a member variable or method to be used externally (i.e. the private feature), you can open it with __ double underscore.

Properties can be defined not only in init, but also in any method defined by self. But it’s better to define it in init.

3. Python can also implement abstract base classes, called interfaces:

Pay attention to: @abstractMethod cannot be invoked on private properties and methods. Otherwise it will fail. Because private cannot be overridden, the generation cannot be instantiated.

4. The __str__ attribute converts an object to a string, that is, a call to print(object) is a string that will be printed.

5. Call (magic method) can call an object as a function. Just give it an input parameter. :

def __call__(self, protoVer):

        return api.protoModules[protoVer].TimeTicks(


Copy the code

What it does: It is more commonly used as a callback because it can hold state information. It is similar to closures, perhaps a little more readable than closures.

6. Can object instances be deleted?

7. Take a look at the Python reference manual.

Python static methods use the decorator syntax @staticMethod.

9. There is another method called to a class: calss.method (object).

If you want to call a method from a subclass, you can call:


But there’s a better way:

Super (child, self).foo() It has the advantage of not explicitly giving the type of the base class.

CLS: the first argument to a class method. Typically represents the type of the class, which can be generated by CLS ().

    def spawn(cls, *args, **kwargs):
        """Return a new :class:`Greenlet` object, scheduled to start.

        The arguments are passed to :meth:`Greenlet.__init__`.
        g = cls(*args, **kwargs)
        return g
Copy the code

If a subclass defines an __init__ function, its init function will not call the parent class’s init function by default. This is different from c++. If a subclass does not define __init__, the subclass calls __init__ of its parent class. And you can see that, in fact, if a subclass defines an init function, it overrides the parent class’s init.

13, super attention:!! It can only be used in modern class definitions. What’s new? The base class is defined to inherit object. .

How to inherit methods: As long as you inherit a class, all methods of that class will be inherited, including __init__,del. But if a subclass overrides a method, it overrides the parent class’s method and never calls the parent class’s method again. If you want to call a method of the parent class, you can call it as super.

15, inheritance how to inherit properties: as long as the parent class __init__ method is not overridden, or the parent class __init__ method is called, will inherit the parent class __init__ property method. These properties can also be changed after inheritance.

A method or attribute that begins with __ prevents it from being inherited.

17. In my experience, Python inheritance can be understood in an essential way: A Python class is a collection of methods, and to inherit a class is to inherit all the methods of that class. If you define a method in a subclass, you are changing the symbol of that class. Properties, on the other hand, can be defined in all methods, as long as you call the method that defines the property, call the parent class, you inherit the property of the parent class, and call the method that defines the property of the child class, you define the method of the child class.

18 and the property:

class c(object):
    def __init__(self):
        self._num = 1
    def num(self):
        return self._num * 10
    def num(self, v):
        self._num = v
    def num(self):
o = c()
o.num = 20
Copy the code

19. The advantage of this is that properties can be manipulated without appearing as method calls and are more readable. At the same time, it can unify the entrance. Note that it must also inherit object.

Garbage collection in OO: Python garbage collection uses symbolic reference counting. So, if I apply for an object in a function and then return one of its properties or methods, will the object be freed when the symbolic reference to the object has been removed?

class child(parent):
    def __init__(self):
        self.i = 8888
    def foo(self):
    def __del__(self):
        print('now in del child')
        super(child, self).__del__()
Copy the code

In the first case, you return a property

def refun():

    o = child()
    return o.i
I = refun()
Copy the code

At this point, object O is immediately released. Because o.i is really just a reference to an object, it has nothing to do with o

In the second case, the method is returned

def refun():
    o = child()
    return o.foo
foo = refun()
Copy the code

At this point, the object o must wait until foo is freed, because foo contains a reference to o (foo’s entry argument self).

20. For attributes of an object, if the attributes are read and write, the first step does not need to be decorated with @property. It can be used directly. If necessary, modify the following. This reduces work and does not change the original code when it is modified.