Home > other >  Python introductory of fifteen
Python introductory of fifteen

Time:09-17

A,

Class is in series with the same characteristics and behavior of things referred to, is an abstract concept, is not the real thing,

Object is a class created real things,

Development, have a class first, and then, the object, eg: the student class, student a object

# define class washing machine "" "the class name of the class () : code ", "class Washer () : def for wash (self) : # self refers to have to call the function object, print (' can wash the clothes') # # object creation object name=class name () heier=Washer () print (heier) heier. Wash ()



Second, magic methods

In python, xx __ __ () function is called magic methods, refers to the function with special functions,

P. : both sides are the two underlined

1, __init__ ()

Initializes the object

Class Washer () : def __init__ (self, high, width) : the self. The high=high self. The width=width def wath (self) : print (f '{self. Width}, the width of the washing machine') print (f 'washing machine highly {self. High}') heier1=Washer (10, 20) heier1. Wath () "" "20, the width of the washing machine washing machine height 10 "" "

2, the __str__ ()

In the use of print output object when the default print object memory address,

If a class defines the __str__ method, it can print the return of the data from this method,

Class Washer () : def __init__ (self) : the self. The high=500 def the __str__ (self) : return 'explanations and instructions or the state of the object of a class' heier1=Washer () print (heier1) "" "explanations, descriptions or category of the state of the object that "" "

3, __del__ ()

But delete the object calls when

Class Washer () : def __init__ (self) : the self. The high=500 def __del__ (self) : print (' object delete) heier1=Washer ()
  • Related