python magic function

hello, Hello, everyone, today to share a little magic python function that comes with Python function double-underlined, although in large-scale software development we use much, unless of course you want to be playing (you can show (installed force) it), although not much use, but if you can use a number of them, then there is no skill basis points is quite difficult, so-called no solid basic skills, even if the Secret Rivals, beating dragon 18 palms, jiuyin bones claw, Qian Kun Da Nuo all clear in mind, still is style and no substance, a hundred no one

 

 

  So honestly put their basic skills needed to train, play with the object-oriented reached a pinnacle, the other is just a little training, learn the recipe with the entertainer can dominate one day. Haha cow blowing too much, or back to the topic, summarize share some slightly less familiar magic method.

A, __ str __ ():

It is represented by the direct printing method object implementation, __ str__ print function is called, are generally returns a value that is represented as strings, to note that you must return a string type, otherwise it will throw an exception:

 

 

 

 As a case in object-oriented, we can go to print the results we want in accordance with their own ideas.

Two, __ call __ ():

Objects through the __call __ () method of the object can be simulated behavior of the function, i.e. specifically defined function we want in __call__ function inside or other information or return a multiple splicing incoming parameters, this effect is we can use the class to use as the same function, but also a very interesting thing. (Ah, like slippers to wear not only)

 

Three, __ repr __ ():

Str with similar, but for str values ​​were converted to human-readable form, but is converted to repr for read by the interpreter, the difference is that the latter () function or to print a print function when the function is in str use, and it returns a string of end-user friendly, if you only want to achieve a use repr would be better, because if time is not an object str function, but they need to call its Python interpreter will be used as an alternative to repr go with the call. Common usage is what I need to output its own data processing time, override this method, then call this print () method or when the repr, we will be able to call up the desired data.

Four, __ setattr __ ():

该方法会拦截所有的赋值语句,如果定义了,self.attr = value就会变成self.__setattr__("attr", value), 需要注意的是当在__setattr__方法里边对属性进行赋值的时候,不能使用self.attr = value, 因为会再次调用self.__setattr__("attr", value),导致堆栈溢出。应该使用self.__dict__['name'] = value即对属性字典做索引运算来赋值。

常规对属性赋值被赋值的属性和值会存入实例属性字典__dict__中

 

 如果自定义了__setattr__,对实例属性的赋值就会调用它。类定义中的self.attr也同样,所以在__setattr__下还有self.attr的赋值操作就会出现无线递归的调用__setattr__的情况。自己实现__setattr__很大风险,一般情况都还是继承object类的__setattr__方法。

 

 

五、 __getattr__():

拦截点号运算。当对未定义属性名称和实例进行点号运算时,就会用属性,名作为字符串调用这个方法,如果继承树可以找到该属性,则不调用。即当我们实例化一个对象,当某属性可以通过正常机制追溯到即在对象.__dict__中可以找到时,不会调用该方法。

 

 六、__delattr__():

需要注意的是和__setattr__一样,适用于删除对象的某个属性时才用到。

 

 七、__eq__():

用来判断两个对象的值是否相等,"=="会触发这个方法。

 

 如果我们不重写的话,会默认调用object中的__eq__方法, 那个比较严格,有时候不好判断两个对象是否相等, 这个会经常进行重写。

好啦, 今天的魔法方法就分享到这吧,当然还有其他的魔法方法, 比如__class__, __doc__, __name__等其他方法,这些方法都是我们比较常用的,也就没必要小猿在这里再啰嗦一遍,希望能给在Python或者其他语言带来点启发, 你的关注和感悟是小猿我分享知识,继续推送的最大动力。大家一起努力。

Guess you like

Origin www.cnblogs.com/f-g-f/p/12142862.html