-2

What I want:

  1. An object that behaves like a float, I mean that support the most operations a float supports so it can be used in any expression a float can be used too.
  2. The object MUST NOT had a value until it's needed.

Some kind of lazy float.

Motivation:

The object represents a value in a Data Base and the real value of the object is in the DB (getting its value could be expensive), also when the object is assigned or under certain operations it must update the value in the DB (like the operation increment by a value, which should be modelled as a method of the object). Remark: if we just want to increment the value, we don't need and don't want to get the real value from the DB.

The question: It is possible to subclass float and overwrite some (or just one) of its methods to achieve the proposed objectives?

Note: please, I know the solution to the problem maybe (almost for sure) is not the best, but I posted it just as an example, It came to my mind trying to answer this question if you want to offer a better solution to the problem post it in the original question, thanks.

Community
  • 1
  • 1
Alvaro Fuentes
  • 16,937
  • 4
  • 56
  • 68
  • Do you have some subset of operators that you want to be able to use it with without referring to the stored value? Or do you just want it generally to work with everything. For example, __bool__ will be troublesome since you can't compare to zero. – KobeJohn Dec 18 '13 at 16:24
  • Why not just use `f = None` and assign it whenever you need? – Timothy Dec 18 '13 at 16:24
  • Is the idea that the lazy float _has_ a value, in principle, but that the program not actually fetch that value until the variable is accessed (because the source of the value invokes some kind of expensive operation)? So it might be initialized with `x = LazyFloat(db_pointer)`, where `db_pointer` is some kind of object that tells `x` how to fetch the initial value when it's requested? – Tim Pierce Dec 18 '13 at 16:27
  • What operations on a float do you imagine wouldn't need its value? – martineau Dec 18 '13 at 16:31
  • @qwrrty yes, something like that – Alvaro Fuentes Dec 18 '13 at 16:36
  • @martineau I don't have idea, the problem is just an example, suppose I just need a reduced subset of the operations – Alvaro Fuentes Dec 18 '13 at 16:37
  • @Skyler the idea comes from trying to get the object from a descriptor in a DB-access object, look at [this question](http://stackoverflow.com/questions/20571533/methods-on-descriptors/20572211?noredirect=1#comment30866071_20572211) for the inspiring problem – Alvaro Fuentes Dec 18 '13 at 16:40
  • @xndrme: If you only want a subset of operations, then you'd need to make the rest of the methods in thefourtheye's [answer](http://stackoverflow.com/a/20663338/355230) raise a `NotImplementedError` exception because otherwise your subclass will inherit them from the base class (and either silently do something wrong or perhaps raise a misleading exception). – martineau Dec 18 '13 at 16:58

1 Answers1

3

What you want to do is called ducktyping. You can do that so easily in Python.

def MyPuppyFloat(float):
...

And then you can emulate float type with the following methods

object.__add__(self, other)
object.__sub__(self, other)
object.__mul__(self, other)
object.__floordiv__(self, other)
object.__mod__(self, other)
object.__divmod__(self, other)
object.__pow__(self, other[, modulo])
object.__lshift__(self, other)
object.__rshift__(self, other)
object.__and__(self, other)
object.__xor__(self, other)
object.__or__(self, other)

object.__div__(self, other)
object.__truediv__(self, other)
object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
object.__rdiv__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
object.__rdivmod__(self, other)
object.__rpow__(self, other)
object.__rlshift__(self, other)
object.__rrshift__(self, other)
object.__rand__(self, other)
object.__rxor__(self, other)
object.__ror__(self, other)

object.__neg__(self)
object.__pos__(self)
object.__abs__(self)
object.__invert__(self)
object.__complex__(self)
object.__int__(self)
object.__long__(self)
object.__float__(self)
object.__oct__(self)
object.__hex__(self)
Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497