python - “覆盖 StructuredNode 构造函数”的问题
问题描述
尝试覆盖 StructuredNode 构造函数时出现此错误,而它与文档中的代码几乎完全相同。
Traceback (most recent call last):
File "/Users/xiao/PycharmProjects/Fooga_New/test/tmp.py", line 48, in <module>
tmp_node = Item(test='test_test_test')
File "/Users/xiao/PycharmProjects/Fooga_New/test/tmp.py", line 45, in __init__
super(Item, self).__init__(self, *args, **kwargs)
File "/Users/xiao/PycharmProjects/python3_venv/lib/python3.6/site-packages/neomodel/core.py", line 203, in __init__
super(StructuredNode, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
这是我的代码:
from neomodel import db, StructuredNode, StringProperty
db.set_connection('bolt://' + 'neo4j' + ':' + '5428' + '@' + '192.168.0.24' + ':' + '7687')
class Item(StructuredNode):
name = StringProperty(unique_index=True)
uid = StringProperty(unique_index=True)
def __init__(self, test, *args, **kwargs):
# self.product = product
kwargs["uid"] = 'g.' + str(test)
kwargs["name"] = test
super(Item, self).__init__(self, *args, **kwargs)
tmp_node = Item(test='test_test_test')
tmp_node.save()
我想知道我是否正确使用了这个?
谢谢。
解决方案
请看一下*args 和 **kwargs?在继续之前。
在您的示例test
中,您可以通过两种方式使用参数:
from neomodel import db, StructuredNode, StringProperty
from neomodel import config
config.DATABASE_URL = 'bolt://neo4j:neo4j@localhost:7687'
# Using `*args`: The first parameter to the constructor defines the `test`
class Item(StructuredNode):
uid = StringProperty(unique_index=True)
name = StringProperty(unique_index=True)
def __init__(self, *args, **kwargs):
if args:
test = args[0]
kwargs['uid'] = 'g.' + str(test)
kwargs['name'] = test
super(Item, self).__init__(**kwargs)
else:
print('no test')
item = Item('test_test_test')
item.save()
# or using `**kwargs`: a entry `test` in kwargs defines the `test` for uid and name
class Kitem(StructuredNode):
uid = StringProperty(unique_index=True)
name = StringProperty(unique_index=True)
def __init__(self, *args, **kwargs):
if kwargs and 'test' in kwargs:
test = kwargs['test']
kwargs.pop('test',None)
kwargs['uid'] = 'g.' + str(test)
kwargs['name'] = test
super(Kitem, self).__init__(**kwargs)
else:
print('no test')
kitem = Kitem(test='test_test_test_k')
kitem.save()
推荐阅读
- html - 如何在 html 页面中展示 html 代码?
- javascript - 如何将文本输入绑定到同一 html 页面上的另一个 div
- docker - Realmlist - IP 有效,但域无效
- regex - 在 MS Word 正则表达式中捕获组
- java - Java无法访问包外资源
- swift - 无法设置可选值,打印 nil
- flutter - Image Picker 默认使用后置摄像头
- javascript - 无法读取数组的映射属性
- ruby-on-rails - 我正在尝试运行 Bundle Install,此错误消息是什么意思?
- python - 通过 Yagmail 使用域电子邮件