[英]python name mangling using json dumps
(Been using python a while but my lingo is weak so bear with me) (曾经使用python一段时间,但是我的术语很虚弱,所以请多多包涵)
I have a bunch of objects I'm adding to each other in python. 我有一堆要在python中添加的对象。 I'm serializing the objects using their
__dict__
attribute (with __init__
function self.__field
sort of thing.) I'm printing out the objects using json.dumps
for a proof of concept for an API port I'm doing. 我正在使用对象的
__dict__
属性(带有__init__
函数self.__field
类的东西)来序列化对象。我正在使用json.dumps
打印对象,以证明我正在做的API端口。 The problem is I'm using name mangling along with python properties so that when I print it looks sort of as follows 问题是我正在使用名称修饰和python属性,因此在我打印时看起来如下
{
"_Type1__field": {
"A": {
"_Type1-1__field": [
{
"_Type1-1-1__field": [
{
"_Type1-1-1-1__field": null,
"field": 1
},
{
"_Type1-1-1-1__field": null,
"field": 4
}
]
},
{
"_Type1-1-2__field": [
{
"_Type1-1-2-1__field1": null,
"_Type1-1-2-1__field2": null,
"_Type1-1-2-1__field3": null,
"field1": {
"field": 3853579331
},
"field2": 1373546537126.0,
"field3": 1373632937126.0
}
]
}
]
}
}
Is it the mangled getting picked up along with the property objects? 它是否与属性对象一起被拾取? The typical object like the Typex-xx (Typex and Typex-x are outer objects) is set up like so
像Typex-xx这样的典型对象(Typex和Typex-x是外部对象)的设置如下
class Type(inheritance):
def __init__(self):
self.__field= None
self.__field2= {}
@property
def field(self):
return self.__field
@deviceId.setter
def field(self, value):
self.__field = value
@property
def field2(self):
return self.__field2
@field2.setter
def field2(self, value):
self.__field2 = value
It's not really a problem, I'd just like to know what's going on and why each field is showing up as None
and with its value. 这并不是真正的问题,我只想知道发生了什么,为什么每个字段都显示为
None
及其值。
You have an XY problem - https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem and moreover you are reinventing the wheel. 您遇到XY问题-https: //meta.stackexchange.com/questions/66377/what-is-the-xy-problem ,而且您正在重新发明轮子。
Use pickle
http://docs.python.org/2/library/pickle.html or better cPickle
http://docs.python.org/2/library/pickle.html#module-cPickle to serialize Python objects. 使用
pickle
http://docs.python.org/2/library/pickle.html或更好的cPickle
http://docs.python.org/2/library/pickle.html#module-cPickle序列化Python对象。
The Python
philosophy is batteries included
so use stdlib
whenever possible. Python
理念是batteries included
因此请尽可能使用stdlib
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.