更改插入数据后类的类类型 [英] Changing the class type of a class after inserted data

查看:114
本文介绍了更改插入数据后类的类类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在python中创建一个类,应该像这样:


  1. 分配的数据,变量(例如 a = exampleclass(data)或只是 exampleclass(data)


  2. 插入数据时,应自动确定数据的某些属性,如果某些属性已满,则会自动...


  3. ...将类别更改为另一个类别


有问题。我如何真正改变类里面的类?例如:



如果我有两个类,一个是 Small_Numbers ,另一个是 Big_numbers ;现在我想要小于1000的任何 small_number 转移到 Big_number ,反之亦然,testcode:

  a = Small_number(50)
type(a)#应返回Small_number。
b = Small_number(234234)
type(b)#应返回Big_number。
c = Big_number(2)
type(c)#应返回Small_number。这是可以做的吗?



$ b < >解决方案

使用工厂方法是解决这个问题的常用方法, ,因为实例化一个类与在Python中调用一个函数是不一样的。



,您可以分配 self .__ class __

  THRESHOLD = 1000 

class Small(object):
def __init __(self,n):
如果n < THRESHOLD:
self.n = n
else:
self .__ class__ = Big
self .__ init __(n)

class Big(object):
def __init __(self,n):
如果n < THRESHOLD:
self .__ class__ = Small
self .__ init __(n)
else:
self.n = n

这可以按预期工作:

 >> a = Small(100)
>>>> type(a)
< class'Small'>
>>>> b =小(1234)
>>>>类型(b)
< class'Big'>
>>>> c = Big(2)
>>>>类型(c)
< class'Small'>

如果分配给 self .__ class __ 陌生,然后您可以覆盖 __新__ 。此方法在调用 __ init __ 之前调用,可用于选择要实例化的类:

  THRESHOLD = 1000 

类Switcher(object):
def __new __(cls,n):
如果n < THRESHOLD:
new_cls = Small
else:
new_cls = Big
instance = super(Switcher,new_cls).__ new __(new_cls,n)
if new_cls!= cls :
实例.__ init __(n)
返回实例

class Small(Switcher):
def __init __(self,n):
self.n = n

class Big(Switcher):
def __init __(self,n):
self.n = n
pre>

I want to create a class in python, which should work like this:

  1. Data assigned, maybe bound to a variable (eg a = exampleclass(data) or just exampleclass(data))

  2. Upon being inserted data, it should automatically determine some properties of the data, and if some certain properties are fullfilled, it will automatically...

  3. ... change class to another class

The part 3 is the part that i have problem with. How do i really change the class inside of the class? for example:

If I have two classes, one is Small_Numbers, and the other is Big_numbers; now I want any small_number smaller than 1000 to be transferred into a Big_number and vice versa, testcode:

a = Small_number(50)
type(a) # should return Small_number.
b = Small_number(234234)
type(b) # should return Big_number.
c = Big_number(2)
type(c) # should return Small_number.

Is this possible to do?

解决方案

Using a factory method is the usual way to solve this, especially since instantiating a class is indistinguishable from calling a function in Python.

However, if you really want, you can assign to self.__class__:

THRESHOLD = 1000

class Small(object):
    def __init__(self, n):
        if n < THRESHOLD:
            self.n = n
        else:
            self.__class__ = Big
            self.__init__(n)

class Big(object):
    def __init__(self, n):
        if n < THRESHOLD:
            self.__class__ = Small
            self.__init__(n)
        else:
            self.n = n

This works as expected:

>>> a = Small(100)
>>> type(a)
<class 'Small'>
>>> b = Small(1234)
>>> type(b)
<class 'Big'>
>>> c = Big(2)
>>> type(c)
<class 'Small'>

If assigning to self.__class__ seems too strange, then you can override __new__ instead. This method is called before __init__ is called and it can be used to pick the class to instantiate:

THRESHOLD = 1000

class Switcher(object):
    def __new__(cls, n):
        if n < THRESHOLD:
            new_cls = Small
        else:
            new_cls = Big
        instance = super(Switcher, new_cls).__new__(new_cls, n)
        if new_cls != cls:
            instance.__init__(n)
        return instance

class Small(Switcher):
    def __init__(self, n):
        self.n = n

class Big(Switcher):
    def __init__(self, n):
        self.n = n

这篇关于更改插入数据后类的类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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