简体   繁体   English

如何检测我的python类实例是强制转换为整数还是浮点数?

[英]How can I detect whether my python class instance is cast as an integer or float?

I have a python class to calculate the number of bits when they have been specified using "Kb", "Mb" or "Gb" notation. 我有一个python类来计算使用“ Kb”,“ Mb”或“ Gb”表示法指定的位数。 I assigned a @property to the bits() method so it will always return a float (thus working well with int(BW('foo').bits) ). 我为bits()方法分配了一个@property ,因此它将始终返回一个float (因此可以与int(BW('foo').bits) )。

However, I am having trouble figuring out what to do when a pure class instance is cast as an int() , such as int(BW('foo')) . 但是,我在弄清楚将纯类实例转换为int()int(BW('foo'))时该怎么办。 I have already defined __repr__() to return a string, but it seems that that code is not touched when the class instance is cast to a type. 我已经定义了__repr__()返回一个字符串,但是当类实例被强制转换为类型时,似乎没有触及该代码。

Is there any way to detect within my class that it is being cast as another type (and thus permit me to handle this case)? 有什么方法可以在我的班级中检测到它正在被强制转换为另一种类型(从而允许我处理这种情况)?

>>> from Models.Network.Bandwidth import BW
>>> BW('98244.2Kb').bits
98244200.0
>>> int(BW('98244.2Kb').bits)
98244200
>>> BW('98244.2Kb')
98244200.0
>>> type(BW('98244.2Kb'))
<class 'Models.Network.Bandwidth.BW'>
>>>
>>> int(BW('98244.2Kb'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'BW'
>>>

Basically what you want lies within overriding __trunc__ and __float__ in the Models.Network.Bandwidth.BW class: 基本上,您想要的是位于Models.Network.Bandwidth.BW类的__trunc____float__中:

#!/usr/bin/python

class NumBucket:

    def __init__(self, value):
        self.value = float(value)

    def __repr__(self):
        return str(self.value)

    def bits(self):
        return float(self.value)

    def __trunc__(self):
        return int(self.value)

a = NumBucket(1092)
print a
print int(a)
print int(a.bits())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该将目标类转换为浮点数还是整数? - Should I cast target class as float or integer? 使用 Python,如何检测程序是最小化还是最大化? - Using Python, how can I detect whether a program is minimized or maximized? Python,如何检测电脑是否处于电池供电状态? - In Python, how can I detect whether the computer is on battery power? 如何将浮点值插入 Python 中的 integer 类型列表中? - How can I insert float values into integer type list in Python? 如何将 ctypes 指针转换为 Python 类的实例 - How to cast a ctypes pointer to an instance of a Python class 在 Python 中可以使用 instance() 来检测 class 方法吗? - In Python can instance() be used to detect a class method? 为什么当我通过列表而不是浮点数或 integer 时,我的 class 实例在每次迭代后保存最终值? - Why is my class instance saving the final value after each iteration when I pass through a list instead of a float or integer? 如何在 python 中将 a.0 浮点数转换为 integer? - How do I turn a .0 float into an integer in python? 我怎样才能以我的程序识别它是 int 还是 float 的方式接受用户输入? - How can I accept the user input in such a way that my program recognizes whether it is an int or a float? 如何在PostgreSQL中将整数解压缩为浮点数? - How can I unpack an integer into a float in PostgreSQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM