Python Object-oriented advanced

Source: Internet
Author: User

Static methods

    • Static methods are just nominal collation management, and in fact no property in a class or instance can be accessed in a static method.
    • static method functions:
      • Turning a method into a static method is equivalent to cutting off its association with a class and does not automatically pass self. is a function.
        • A. The parameter self is not passed in when the method is created.
        • B. If you must pass in the parameter, you need to pass the instantiated object to yourself when calling the method to execute.
      • The method must be called through the class name. Method ().
#!/usr/bin/python#-*-coding:utf-8-*-"""static methods: Static methods are just nominal collation management, and virtually no property in a class or instance can be accessed in a static method. The effect of static methods: 1. Turning a method into a static method is equivalent to cutting off its association with the class and not automatically passing self. is a function.    A. The parameter self is not passed in when the method is created. B. If you must pass in the parameter, you need to pass the instantiated object to yourself when calling the method to execute. 2. The method must be called through the class name. Method ()."""classDog (object):def __init__(self, name): Self.name=name @staticmethoddefEat ():#TODO So wouldn't it be inconvenient if the method needed parameters?        Print("A is eating") @staticmethoddefEat (self, food):#when this method is called, the instance must be passed to itself, and self is not automatically passed in. That is D.eat (d, "bun")        Print("%s is eating%s"%(self.name, food)) d= Dog ("Chenronghua") D.eat ("steamed Bun")
Static Methods

Class method

  • Only class variables (global variables) can be accessed and instance variables cannot be accessed
  • """class methods: Only class variables (global variables) can be accessed and instance variables cannot be accessed"""classDog (object): N= 333def __init__(self, name): Self.name=name @classmethoddefEat (self):#when this method is called, the instance must be passed to itself, and self is not automatically passed in. That is D.eat (d, "bun")        Print("%s is eating%s"% (SELF.N,'DD')) d= Dog ("Chenronghua") d.eat ()
    class Method

Property method

  • Turn a method into a static property. You cannot add () when calling this method, to instantiate the object name directly, as if a variable was called. Method name.
  • Parameter cannot be passed when calling this method (because there is no ())
  •  
      
    • You need to write a function with the same name (this function must be placed below the Comment property function) and annotated with the @ method name. Setter. (a bit like the set method for private variables in Java)
    • Assigning values to eat after instantiation
  • To delete this attribute, you need to write another method with the same name. The comment is the @ method name. deleter.
  • """Property Methods: 1. Turn a method into a static property. You cannot add () when calling the method, and you want to instantiate the object name directly, as if you were calling a variable. The name of the method. 2. A parameter cannot be passed when calling the method (because there is no ()) 3.         Can be assigned a value. A. You need to write a function with the same name (this function must be placed below the Comment property function) and annotated with the @ method name. Setter. (a bit like the set method of a private variable in Java) B. Eat is assigned a value of 4 after instantiation. To delete this attribute, you need to write another method with the same name. The comment is the @ method name. deleter."""classDog (object):def __init__(self, name): Self.name=name self.__food=None @property#Turn a method into a static property    defEat (self):#when this method is called, the instance must be passed to itself, and self is not automatically passed in. That is D.eat (d, "bun")        Print("%s is eating%s"% (Self.name, self.__food) ) @eat. Setter#this method with the same name must be written under the previous eat () method. Modifies a property.    defeat (self, food):Print("set to food:%s"%Food ) self.__food=Food @eat. Deleter#deletes an attribute.    defEat (self):delSelf.__food        Print("deleted") d= Dog ("Chenronghua")#d.eat () # returns ' Nonetype ' object is not callableD.eat#returns Chenronghua is eating ddD.eat ="steamed Bun"  #must have @eat.setter method, in order to assign a value, otherwise error.D.eat#back Chenronghua is eating bun
    Property Method

Special member methods for classes

    • __doc__
      • Represents the description information for a class.
      • If there is a comment below the class, the description information of the class can be printed by using the Print method.
class Dog (object):     """     This is a description of    the message """print Dog.  __doc__  #  returns "  This is a descriptive message"
__doc__
    • __module__
      • indicates what module the object of the current operation is in
    • __class__
      • What is the class of the object that is currently being manipulated
      • __init__
        • Constructs a method that automatically triggers execution when an object is created through a class
      • __del__
        • destructor method, when the object is freed in memory, Automatic trigger execution
        • Note: This method is generally not defined, because Python is a high-level language, and programmers do not need to care about the allocation and deallocation of memory as this work is done with the Python interpreter, so the destructor is called by the interpreter automatically when it is garbage collected Triggers the execution of the.
        • The
      • __call__
        • object is appended with parentheses, triggering execution
        • Note: The execution of the construction method is triggered by the creation object, that is: Object = class name (), and for the execution of the __call__ method, after the object Parentheses trigger, that is: Object ()   or Class ().
      • __dict__
        • View all members of a class or object
          • is called through a class: Prints all properties in the class, excluding instance properties
          • calling through objects, printing the There are instance properties, not including class properties
    • __str__
      • If the __str__ method is defined in a class, the return value of the method is output by default when the object is printed.
    • __getitem__, __setitem__, __delitem__
      • Used for index operations, such as dictionaries. Each of the above represents get, set, delete data (like set in Java, Get method)
      • Can be implemented to encapsulate a dictionary as an instance, and users can not arbitrarily delete the properties (key)
    • __new__ \ __metaclass__

Python Object-oriented advanced

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.