Python作业第七天

# 作业1:
# 定义一个类:Person类, name , age
# person = Person("", 19)
# print(person) => ""
# bool(person) => 返回False
# len(person) => 1024
# person2 = Person("", 20)
# person == person2 #如果两个年龄相等返回True, 不相等返回False
# person < person2  #如果person年龄> person2的年龄,返回False, 否则 返回True
# person + person2: 返回: person的名字和person2的名字
class Person(object):
    name = None
    age = None

    def __init__(self, name, age):
        self.name = name
        self.age = age
        # print(f"My name is {name}, age is {age}")

    def __str__(self):
        return f"My name is {self.name}, age is {self.age}"

    def __bool__(self):
        return False

    def __len__(self):
        return 1024

    def __eq__(self, other):
        if self.age == other.age:
            return True
        else:
            return False

    def __lt__(self, other):
        if self.age > other.age:
            return False
        else:
            return True

    def __add__(self, other):
        return self.name + "," + other.name


person = Person("zf", 24)
print(person)  #
# data = str(person)
# print(data)
print(100 * "-")
print(bool(person))  #
print(100 * "-")
print(len(person))  #
print(100 * "-")
person2 = Person("pt", 23)
print(person == person2)  # print(person.__eq__(person2))
print(100 * "-")
print(person < person2)  # print(person.__lt__(person2))
print(100 * "-")
print(person + person2)  # print(person.__add__(person2))
print(100 * "-")
# 作业2:
# 定义一个装饰器:计算函数执行时间: 使用模块time
# time.sleep(2)
# #计算函数运行时间;
# import time
# print(time.time())
# time.sleep(2)
# print(time.time())
import time


def my_decorator(func):
    data = func()

    def decorator():
        time.sleep(2)
        data()
        time.sleep(2)

    return decorator


# @my_decorator
def to_decorator():
    print(time.time())


to_decorator()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值