开发者

Python automatic class assignment

In python. when I write x = 5, x becomes an instance of int automatically. But suppose I have defined a new class say number and I want x to become an instance of number instead of int when I assign it the value 5. Is this possible?

ie, Instead of this -->

>>> x = 5
>>> type(x)
<type 'int'>

Is this possible:

>>> x = 5
>>> type(x)
&开发者_如何学Clt;type 'number'>


No. You would have to write a monkey patch to achieve this, that is incredibly unpythonic, can you simply not write

x = number(5)

:)


Note that you really should never do something like this. Jakob has the right answer, i.e. use x = number(5).

However, that said, I wanted to try how it could be done in theory, and here's one solution in the form of a decorator:

import types

class number(object):
    def __init__(self, value):
        self.value = value

def replace_int(x):
    if isinstance(x, int):
        return number(x)
    else:
        return x

def custom_numbers(f):
    code = f.func_code
    consts = tuple(map(replace_int, code.co_consts))
    new_code = types.CodeType(code.co_argcount, code.co_nlocals,
                              code.co_stacksize, code.co_flags,
                              code.co_code, consts, code.co_names,
                              code.co_varnames, code.co_filename,
                              code.co_name, code.co_firstlineno,
                              code.co_lnotab)
    return types.FunctionType(new_code, f.func_globals, f.func_name)

Any function you decorate, will end up using your custom number class:

@custom_numbers
def test():
    x = 5
    print type(x)


>>> test()
<class '__main__.number'>

The decorator works by replacing integer constants from the function's code-object with instances of the custom class. However, since function.co_code and code.co_consts are both read-only attributes, we have to create new code and function objects with the altered values.

One caveat is, that the values are assumed to be constants, so new instances are not created for each invocation of the function. If you mutate the value, that new value will be reflected in each subsequent call of the function.


You would have to take advantage of Python's language services to compile the statement and then walk the AST replacing the objects as appropriate.


In fact, 5 is an instance of int, x is just pointing to it. All variables in Python are references to objects. Thus, when you write type(x) you get the type of the object which x holds a reference to, in this case it is int.

If you assign another value to x, say x = "string", x will hold a reference to that string object, and type(x) will return <type 'str'>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜