简体   繁体   English

将变量设置为类的类型

[英]Set variable as type of class

I am trying to figure out how I can pass a variable as the declaration type (object) for a class in Python 3. 我试图弄清楚如何将变量作为Python 3中类的声明类型(对象)传递。

Example: 例:

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

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

I tried passing an instance of the MongoEngine variable as a variable to the TestClass but this isn't working properly? 我尝试将MongoEngine变量的实例作为变量传递给TestClass但这不能正常工作?

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 . 不要将Document放在类定义中,就好像TestClassDocument的子类一样。 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: 相反,将类声明为standard (object) ,并定义__init__ ,您可以在其中传递一个变量,该变量可以在初始化后由该类的实例使用:

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()

[EDIT based on comment] [根据评论编辑]

OP's comment: OP的评论:

Looking at the type(test_instance) , Its not the same as a MongoEngine.Document . 查看type(test_instance) ,它与MongoEngine.Document I am hoping to create a class of type 'Document' and pass in an instance of that type? 我希望创建一个类型为'Document'的类并传递该类型的实例?

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 由于我不知道MongoEngine我将以list为例

A class defined as follows, will behave perfectly like a list , but if you do a type() it will come back as MyList : 如下定义的类,其行为将完全类似于list ,但是如果您执行type() ,它将以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 : 使用此类时,您可以轻松地看到它,首先将其视为一个list

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

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

this will behave as if it was a list . 这将像list

But when you do a type() , it will not return the same as list : 但是当您执行type() ,它不会返回与list相同的结果:

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

output: 输出:

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

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. 因此,即使您尝试使用MongoEngine.Document作为父对象创建一个类, type()仍然会向您显示您自己定义的类。

class MyClass(MongoEngine.Document):

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

my_instance = MyClass('something')

If you do a type(my_instance) it will return your custom class, and not the parent object type. 如果您执行type(my_instance) ,它将返回您的自定义类,而不是父对象类型。

Not sure how MongoEngine works, and if you can actually do something like this, so YMMV. 不确定MongoEngine的工作方式,是否可以执行类似的操作,所以不知道YMMV。

You can change the name type() is returning, by doing the following in my example class. 您可以通过在示例类中执行以下操作来更改type()返回的名称。 Setting the self.__class__ in the __init__() . __init__()设置self.__class__ 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))

output: 输出:

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

If this trick works for MongoEngine.Document I do not know. 如果这个技巧对MongoEngine.Document有效,我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM