class Int:
def __init__(self, name):
self.name = name
self.value = None
def __str__(self):
return self.value
class String:
def __init__(self, name):
self.name = name
self.value = None
def __str__(self):
return self.value
class Cout:
def __lshift__(self, other):
print(other, end='')
return Cout()
class Cin:
def __rshift__(self, other):
other.value = input()
if isinstance(other, Int):
other.value = int(other.value)
globals()[other.name] = other.value
return Cin()
endl = '\n'
cout = Cout()
cin = Cin()
cin >> Int('a')
cin >> String('b')
print(a, type(a))
print(b, type(b))
【Python】python魔法函数实现c++的cin,cout
最新推荐文章于 2024-04-30 17:28:01 发布