将变量设置为类的类型 [英] Set variable as type of class

查看:91
本文介绍了将变量设置为类的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何将变量作为Python 3中类的声明类型(对象)传递。

I am trying to figure out how I can pass a variable as the declaration type (object) for a class in Python 3.

示例:

#class defintion
class TestClass(Document):
    test = IntField()

me = MongoEngine(app)
testInstance = TestClass(me.Document) # How do i pass the Document variable

我尝试将 MongoEngine 变量的实例作为变量传递给 TestClass ,但这不是'不能正常工作?

I tried passing an instance of the MongoEngine variable as a variable to the TestClass but this isn't working properly?

推荐答案

我认为您需要在结构上稍有不同。不要将 Document 放在类定义中,就像 TestClass Document的子类一样。而是将类声明为标准的(object),并定义一个 __ init __ ,您可以在其中传递一个变量,该变量可以初始化后由类的实例使用:

I think you need to structure your class slightly different. Don't put Document in the class definition as if the TestClass is a subclass of Document. In stead, declare the class as standard (object), and define an __init__ where you can pass a variable which can be used by the instance of the class after initiation:

class TestClass(object):

    def __init__(self, my_document):
        self.document = my_document
        # at this point  the self.document variable
        # is the same as the variable passed
        # when initiating the instance of the class

    def show_document(self):
        # do something with your document
        print(self.document)

me = MongoEngine(app)

# this will call __init__() passing the variable
test_instance = TestClass(me.Document)

# now do something with the class intance
test_instance.show_document()

[根据评论编辑]

OP的评论:


查看 type(t est_instance),它与
MongoEngine.Document 不同。我希望创建一个类型为'Document'
的类并传入该类型的实例?

Looking at the type(test_instance), Its not the same as a MongoEngine.Document. I am hoping to create a class of type 'Document' and pass in an instance of that type?

创建将父类作为类定义中的对象的类。因为我不知道 MongoEngine 我将用 list

You can create classes which would take a parent class as object in the class definition. As I do not know MongoEngine I will make an example with list

如下所示的类,其行为将完全类似于 list ,但是如果您执行 type()它将返回为 MyList

A class defined as follows, will behave perfectly like a list, but if you do a type() it will come back as MyList:

class MyList(list):

    def __init__(self, *args, **kwargs):
        super(MyList, self).__init__(*args, **kwargs)

    def my_extra_function(self):
        print('hello world')

您可以轻松看到在使用此类时,首先将其视为列表

You can easily see this when using this class, first look at it as a list:

my_instance = MyList([1, 2, 3])

print(my_instance)
print(my_instance[::-1])

它的行为就像是列表

但是当您执行 type()时,它不会返回与 list 相同的结果:

But when you do a type(), it will not return the same as list:

print(type(list))
print(type(list()))
print(type(MyList()))
print(type(my_instance))

输出:

<class 'type'>
<class 'list'>
<class '__main__.MyList'>
<class '__main__.MyList'>

因此,即使您尝试使用 MongoEngine.Document 作为父对象, type()仍将显示您自己定义的类。

So even when you try to create a class with the MongoEngine.Document as parent object, the type() will still show you your own defined class.

class MyClass(MongoEngine.Document):

    def __init__(self, *args, **kwargs):
        super(MyClass, self).__init__(*args, **kwargs)

my_instance = MyClass('something')

如果您执行 type(my_instance),它将返回您的自定义类,而不是父对象类型。

If you do a type(my_instance) it will return your custom class, and not the parent object type.

不确定MongoEngine的工作方式,如果真的可以执行类似的操作,那么YMMV。

Not sure how MongoEngine works, and if you can actually do something like this, so YMMV.

您可以更改名称 type ()通过在示例类中执行以下操作返回。在 __ init __()中设置 self .__ class __ 。像这样:

You can change the name type() is returning, by doing the following in my example class. Setting the self.__class__ in the __init__(). Like this:

class MyList(list):

    def __init__(self, *args, **kwargs):
        super(MyList, self).__init__(*args, **kwargs)
        self.__class__ = type('list', (list,),{})

    def my_extra_function(self):
        print('hello world', self)

my_instance = MyList([1, 2, 3])

print(type(list))
print(type(list()))
print(type(MyList()))
print(type(my_instance))

输出:

<class 'type'>
<class 'list'>
<class '__main__.list'>
<class '__main__.list'>

如果此技巧适用于 MongoEngine.Document 我不知道。

If this trick works for MongoEngine.Document I do not know.

这篇关于将变量设置为类的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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