在对象中存储带限制的值 [英] Storing value with limits in object

查看:63
本文介绍了在对象中存储带限制的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试限制对象存储的值(int或float):


class Limited(对象):

def __init__ (自我,价值,最小,最大):

self.min,self.max = min,max

self.n = value

def set_n(self,value):

if value< self.min:#boundary check

self.n = self.min

if value self.max:

self.n = self。 max

else:

self.n = value

n = property(lambda self:self._value,set_n)


这是有效的,除了我希望这个类的行为类似于内置类型,所以

我可以像这样使用它:


a =有限(7,0,10)

b = math.sin(a)


因此该对象本身返回它的值(这是存储在a)中。这是

可能吗?

解决方案

2008年6月22日星期日上午11:44, Josip< fa ******* @ noone.bewrote:


我试图限制对象存储的值(int或float) :


class Limited(对象):

def __init __(自我,价值,最小,最大):

self.min ,self.max = min,max

self.n = value

def set_n(self,value):

如果值< self.min:#boundary check

self.n = self.min

if value self.max:

self.n = self。 max

else:

self.n = value

n = property(lambda self:self._value,set_n)


这是有效的,除了我希望这个类的行为类似于内置类型,所以

我可以像这样使用它:


a =有限(7,0,10)

b = math.sin(a)


因此该对象本身返回它的值(这是存储在a)中。这是

可能吗?



与普通变量不同,因为=是Python中的重绑定运算符,

而不是分配。


你可以用对象属性做(接近)上面的内容。


大卫。


不是普通的变量,因为=是Python中的重新绑定运算符,


而不是赋值。


你可以用对象属性做(接近)上面的内容。


David。



是的,但它是使用int和float等内置类型完成的。我怀疑我可以从他们那里获得
子类并实现限制,但我必须为每种类型制作

单独的类。我可以在我的班级中覆盖转换方法,例如int()

和float()吗?


On Sun,2008年6月22日12:下午24点,Josip< fa ******* @ noone.bewrote:


>与正常的vars不同,因为=是Python中的重新绑定操作符,而不是赋值。

你可以使用对象属性(接近)上面的内容。

大卫。



是的,但它是使用int和float这样的内置类型完成的。我怀疑我可以从他们那里获得
子类并实现限制,但我必须为每种类型制作

单独的类。我可以在我的班级中覆盖转换方法,例如int()

和float()吗?



我想我可能误读了你的原帖。


整数和浮点数是内部的,不可变的类型,顶部有一些类

善(所以你可以像对象一样对待它们,

来自它们的子类等)。 Python的解释器具有内置逻辑

,它知道如何使用整数和浮点变量,而无需调用

他们的特殊__方法。如果以这种方式工作,那么Python会慢很多。


为了做到你想要的,你需要添加一个新的内部数字

类型为Python。


您可以从float子类化,并重新定义__float__和__int__,但

只会被调用当你的代码实际调用内置的

float()和int()内置时,例如:


导入数学


class Float(float):

def __float __(self):

raise NotImplementedError


a = Float(1)

打印math.sin(a)


#输出0.841470984808

a = Float(1)

print math.sin(float(a))


#引发NotImplementedError异常


没办法(afaik)一个对象告诉Python调用

之一的方法来获取引用,或者''value''到对象(如果有
是,它会让事情变得非常复杂)。在Python中你通常需要更新查找期间使用的逻辑以获得该效果(即,在abc中使用
,你可以自定义ab查找,或者abc查找,但是

不是查找本身。)

从理论上讲,你可以破解Python的内部本地人或全局数据

字典,以便它在查找你的

对象时做了一些不寻常的事情。但实际上这不起作用,因为返回的

对象(当你调用globals()或locals())属性是readonly。

可能是因为那些内部查询dicts是在
优化的C而不是Python中实现的,而C实现并不允许你通过Python界面重新定义它的内部结构。


David。


I''m trying to limit a value stored by object (either int or float):

class Limited(object):
def __init__(self, value, min, max):
self.min, self.max = min, max
self.n = value
def set_n(self,value):
if value < self.min: # boundary check
self.n = self.min
if value self.max:
self.n = self.max
else:
self.n = value
n = property(lambda self : self._value, set_n)

This works, except I would like the class to behave like built-in types, so
I can use it like this:

a = Limited(7, 0, 10)
b = math.sin(a)

So that object itself returns it''s value (which is stored in a.n). Is this
possible?

解决方案

On Sun, Jun 22, 2008 at 11:44 AM, Josip <fa*******@noone.bewrote:

I''m trying to limit a value stored by object (either int or float):

class Limited(object):
def __init__(self, value, min, max):
self.min, self.max = min, max
self.n = value
def set_n(self,value):
if value < self.min: # boundary check
self.n = self.min
if value self.max:
self.n = self.max
else:
self.n = value
n = property(lambda self : self._value, set_n)

This works, except I would like the class to behave like built-in types, so
I can use it like this:

a = Limited(7, 0, 10)
b = math.sin(a)

So that object itself returns it''s value (which is stored in a.n). Is this
possible?

Not with normal vars, because = is a rebinding operator in Python,
rather than assignment.

You can do (close to) the above with object properties.

David.


Not with normal vars, because = is a rebinding operator in Python,

rather than assignment.

You can do (close to) the above with object properties.

David.

Yes, but it''s done with built-in types like int and float. I suspect I could
subclass from them and implement limits, but I would have to make
seperate class for each type. Can I override conversion methods like int()
and float() within my class?


On Sun, Jun 22, 2008 at 12:24 PM, Josip <fa*******@noone.bewrote:

>Not with normal vars, because = is a rebinding operator in Python,
rather than assignment.

You can do (close to) the above with object properties.

David.


Yes, but it''s done with built-in types like int and float. I suspect I could
subclass from them and implement limits, but I would have to make
seperate class for each type. Can I override conversion methods like int()
and float() within my class?

I think I may have misread your original post.

ints and floats are internal , immutable types, with some class
goodness on top (so you can treat them like objects to a degree,
subclass from them, etc). Python''s interpreter has built-in logic
which ''knows'' how to use ints and floats variables, without calling
their special "__" methods. Python would be a lot slower if it worked
this way.

To do exactly what you want, you''d need to add a new internal numeric
type to Python.

You can subclass from float, and redefine __float__ and __int__, but
those will only be called when your code actually calls the builtin
float() and int() builtins, eg:

import math

class Float(float):
def __float__(self):
raise NotImplementedError

a = Float(1)
print math.sin(a)

# Outputs 0.841470984808

a = Float(1)
print math.sin(float(a))

# Throws a NotImplementedError exception

There is no way (afaik) for an object to tell Python to call one of
it''s methods to get a reference, or ''value'' to the object (if there
was, it would make things really complicated). In Python you generally
need to update the logic used during a lookup to get that effect (ie,
in a.b.c, you can customise the a.b lookup, or the a.b.c lookup, but
not the a lookup itself).

In theory you could hack Python''s internal locals or globals
dictionary so that it did something unusual while looking up your
object. But in practice this doesn''t work, because the returned
objects (when you call globals() or locals()) attributes are readonly.
Probably because those internal lookup dicts are implemented in
optimized C not Python, and the C implementation doesn''t let you
redefine it''s internals via the Python interface.

David.


这篇关于在对象中存储带限制的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆