python中分离四位整数_如何在python3中模拟4位整数?

这不如@martijn pieters的answer聪明,但它似乎在Python2.7和3.*上工作,而我在Python2.7上得到了AttributeError: 'wrapper_descriptor' object has no attribute '__module__'。在import sys

lt_py3 = sys.version_info < (3,)

lt_py33 = sys.version_info < (3, 3)

class Int(int):

'''

int types

'''

def __new__(self, val=0):

return int.__new__(self, val & (1 << self.bits - 1) - 1)

def __max_type_bits(self, other):

'''

determine the largest type and bits available from those in `self` and

`other`

'''

if hasattr(other, 'bits'):

if self.bits < other.bits:

return type(other), other.bits

return type(self), self.bits

def __unary_typed(oper):

'''

return a function that redefines the operation `oper` such that the

result conforms to the type of `self`

'''

def operate(self):

return type(self)(oper(self))

return operate

def __typed(oper):

'''

return a function that redefines the operation `oper` such that the

result conforms to the type of `self` or `other`, whichever is larger

if both are strongly typed (have a `bits` attribute); otherwise return

the result conforming to the type of `self`

'''

def operate(self, other):

typ, bits = self.__max_type_bits(other)

return typ(oper(self, other))

return operate

def __unary_ranged(oper):

'''

return a function that redefines the operator `oper` such that the

result conforms to both the range and the type of `self`

'''

def operate(self, other):

'''

type and bitmask the result to `self`

'''

return type(self)(oper(self) & (1 << self.bits - 1) - 1)

return operate

def __ranged(oper):

'''

return a function that redefines the operator `oper` such that the

result conforms to both the range and the type of `self` or `other`,

whichever is larger if both are strongly typed (have a `bits`

attribute); otherwise return the result conforming to the type of

`self`

'''

def operate(self, other):

'''

type and bitmask the result to either `self` or `other` whichever

is larger

'''

typ, bits = self.__max_type_bits(other)

return typ(oper(self, other) & (1 << bits - 1) - 1)

return operate

# bitwise operations

__lshift__ = __ranged(int.__lshift__)

__rlshift__ = __ranged(int.__rlshift__)

__rshift__ = __ranged(int.__rshift__)

__rrshift__ = __ranged(int.__rrshift__)

__and__ = __typed(int.__and__)

__rand__ = __typed(int.__rand__)

__or__ = __typed(int.__or__)

__ror__ = __typed(int.__ror__)

__xor__ = __typed(int.__xor__)

__rxor__ = __typed(int.__rxor__)

__invert__ = __unary_typed(int.__invert__)

# arithmetic operations

if not lt_py3:

__ceil__ = __unary_typed(int.__ceil__)

__floor__ = __unary_typed(int.__floor__)

__int__ = __unary_typed(int.__int__)

__abs__ = __unary_typed(int.__abs__)

__pos__ = __unary_typed(int.__pos__)

__neg__ = __unary_ranged(int.__neg__)

__add__ = __ranged(int.__add__)

__radd__ = __ranged(int.__radd__)

__sub__ = __ranged(int.__sub__)

__rsub__ = __ranged(int.__rsub__)

__mod__ = __ranged(int.__mod__)

__rmod__ = __ranged(int.__rmod__)

__mul__ = __ranged(int.__mul__)

__rmul__ = __ranged(int.__rmul__)

if lt_py3:

__div__ = __ranged(int.__div__)

__rdiv__ = __ranged(int.__rdiv__)

__floordiv__ = __ranged(int.__floordiv__)

__rfloordiv__ = __ranged(int.__rfloordiv__)

__pow__ = __ranged(int.__pow__)

__rpow__ = __ranged(int.__rpow__)

class Int4(Int):

bits = 4

x, y = Int4(10), Int4(9)

print(x + y)

print(x*y)

在名为answer.py的文件中运行此代码将产生

^{pr2}$

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值