兑对象的引用
# !/usr/bin/python3# -*- coding: utf-8 -*-
# @Time : 2023/2/22 21:56
# @Author : xiongming
# @File : tiaoyongDuiXiang.py
# @Desc : 调用对象
class A:
# 一个*号位置参数,两个**代表关键字参数
def __call__(self, *args, **kwargs):
print("嗨~~")
print(f"位置参数-->{args}\n关键字参数-->{kwargs}")
a = A()
a(1, 2, 3, x = 250, y = 288)
class Power:
def __init__(self, exp):
self.exp = exp
def __call__(self, base):
return base ** self.exp
square = Power(2)
print(square(2))
cube = Power(3)
print(cube(2))
# 字符串 魔法方法
print(eval("1 + 2"))
class C:
def __init__(self, data):
self.data = data
def __str__(self):
return f"data = {self.data}"
def __repr__(self):
return f"C({self.data})"
def __add__(self, other):
self.data += other
c = C(250)
print(c)
c + 250
print(c)
页:
[1]