zoukankan      html  css  js  c++  java
  • Operator overloading

    By defining other special methods, you can specify the behavior of operators on user-defined types. For example, if you define add method for the Time class, you can use the + operator on Time objects.

     def __add__(self,time):
            seconds = self.time_to_int() + time.time_to_int()
            return Time.int_to_time(seconds)

    When you apply the + operator to Time objects, Python invokes __add__. When you print the result, Python invokes __str__. So there is quite a lot happening behind the scenes. Changing the behavior of an operator so that it works with user-defined types is called operator overloading. For every operator in Python there is a corresponding special method, like __add__.

     

    from Thinking in Python

     

  • 相关阅读:
    day3
    day2
    day1-存储
    day5-iptables
    MySQL之补充
    11.18
    11.17
    junit基础学习之-测试controller层(2)
    junit基础学习之-简介(1)
    外键和级联
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/4004431.html
Copyright © 2011-2022 走看看