将变量设置为 class 类型
Set variable as type of class
我想弄清楚如何将变量作为 Python 中的 class 的声明类型(对象)传递 3.
示例:
#class defintion
class TestClass(Document):
test = IntField()
me = MongoEngine(app)
testInstance = TestClass(me.Document) # How do i pass the Document variable
我尝试将 MongoEngine
变量的实例作为变量传递给 TestClass
,但这无法正常工作?
我认为您的 class 结构需要略有不同。不要将 Document
放在 class 定义中,就好像 TestClass
是 Document
的子 class。相反,将 class 声明为标准 (object)
,并定义一个 __init__
,您可以在其中传递一个变量,该变量可在启动后由 class 的实例使用:
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()
[根据评论编辑]
楼主的评论:
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?
您可以创建 classes,它将父 class 作为 class 定义中的对象。因为我不知道 MongoEngine
我会用 list
举个例子
A class 定义如下,将表现得像 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')
使用这个class的时候可以很容易的看到,先看成list
:
my_instance = MyList([1, 2, 3])
print(my_instance)
print(my_instance[::-1])
这将表现得像 list
。
但是当你做 type()
时,它不会 return 与 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
作为父对象创建 class,type()
仍会显示您自己定义的 class。
class MyClass(MongoEngine.Document):
def __init__(self, *args, **kwargs):
super(MyClass, self).__init__(*args, **kwargs)
my_instance = MyClass('something')
如果您执行 type(my_instance)
,它将 return 您的自定义 class,而不是父对象类型。
不确定 MongoEngine 是如何工作的,如果你真的可以做这样的事情,所以 YMMV。
您可以将名称 type()
更改为 returning,方法是在我的示例 class 中执行以下操作。在 __init__()
中设置 self.__class__
。像这样:
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
我不知道。
我想弄清楚如何将变量作为 Python 中的 class 的声明类型(对象)传递 3.
示例:
#class defintion
class TestClass(Document):
test = IntField()
me = MongoEngine(app)
testInstance = TestClass(me.Document) # How do i pass the Document variable
我尝试将 MongoEngine
变量的实例作为变量传递给 TestClass
,但这无法正常工作?
我认为您的 class 结构需要略有不同。不要将 Document
放在 class 定义中,就好像 TestClass
是 Document
的子 class。相反,将 class 声明为标准 (object)
,并定义一个 __init__
,您可以在其中传递一个变量,该变量可在启动后由 class 的实例使用:
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()
[根据评论编辑]
楼主的评论:
Looking at the
type(test_instance)
, Its not the same as aMongoEngine.Document
. I am hoping to create a class of type 'Document' and pass in an instance of that type?
您可以创建 classes,它将父 class 作为 class 定义中的对象。因为我不知道 MongoEngine
我会用 list
A class 定义如下,将表现得像 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')
使用这个class的时候可以很容易的看到,先看成list
:
my_instance = MyList([1, 2, 3])
print(my_instance)
print(my_instance[::-1])
这将表现得像 list
。
但是当你做 type()
时,它不会 return 与 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
作为父对象创建 class,type()
仍会显示您自己定义的 class。
class MyClass(MongoEngine.Document):
def __init__(self, *args, **kwargs):
super(MyClass, self).__init__(*args, **kwargs)
my_instance = MyClass('something')
如果您执行 type(my_instance)
,它将 return 您的自定义 class,而不是父对象类型。
不确定 MongoEngine 是如何工作的,如果你真的可以做这样的事情,所以 YMMV。
您可以将名称 type()
更改为 returning,方法是在我的示例 class 中执行以下操作。在 __init__()
中设置 self.__class__
。像这样:
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
我不知道。