python中对象的持久化 [英] persistence of objects in python

查看:82
本文介绍了python中对象的持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我只用 C 语言完成了过程编程,所以我对 OOP 和 Python 都不太了解.

Till now I have done only procedural programming in C so I am rather unclear about both OOP and Python.

我有一个包含许多 python 文件的大型项目.文件 a.py 定义了一个名为 fobject 的类我正在使用 python 2.5

I have a large project which contains a number of python files. File a.py defines a class called fobject I am using python 2.5

文件 b.py 和 c.py 有名为 BProject 和 CProject 的类,它们有一个 fobject 对象作为参数.我在 b.py 中使用了 import CProject(在 c.py 中定义).我在 CProject 中有一个列表,我使用 wx python GUI 填充.接下来我调用 BProject 中定义的函数 BRun,它在内部调用 CProject 中的 CRun 函数,即.在 c.py.

File b.py and c.py have classes called BProject and CProject which have an object of fobject as parameter. I have included using import CProject (defined in c.py) in b.py. I have a list in CProject which I fill using wx python GUI. Next I call a function BRun defined in BProject which internally calls a CRun Function in CProject ie. in c.py.

在此 CRun 中,我想操作列表,但此时列表始终为空.为什么会这样?

In this CRun I want to manipulate the list but list is always empty at this time. Why is this so?

鉴于约束是我无法更改定义 fobject 的任何 a.py,我该怎么办?

What should I do given the constraint is I can't change anything a.py in which fobject is defined ?

def Instance(fObject):
    return test_page_CProject(fObject)


class CProject(object):

    def __init__(self, fObject):
        self.fObj = fObject
        self.IntList  =  []
        ##snip


    def  OnIntSelectBtnPress(self,parent):
        print ":self.IntList"
        print self.IntList
        self.listBoxIntSelect.InsertItems(self.IntList,0)
        print self.IntList


    def OnIntRun(self):

                IntLModeList = self.IntListIntMode
                #snip

文件 b.py

def Instance(fObject):
    return BProject(fObject)

class BProject(object):

    def __init__(self, fObject):

        self.FObj = fObject
        #snip
        Object = __import__('CProject')
        #snip

        self.intObject = Object.Instance(self.FObj)
        self.intObject.OnIntRun()

推荐答案

你不懂 python.你需要了解变量(python中几乎所有东西基本上都是一个指针)和命名空间.

You don't understand python. You need to understand variables (almost everything in python is basically a pointer), and namespaces.

好的,所以你不明白 python 什么时候创建新对象.

OK, so you don't understand when python creates new objects.

在 90% 的情况下,每次您看到一个对象时,它都会是一个新对象.

In 90% of cases, every time you see an object, it will be a new one.

例如:

a = [1,2,3]
b = a + [4,5] 
print a
>>> [1,2,3]

看到了吗?b 是一个新对象,a 没有被触及.

See? b is a new object, a is untouched.

还有:

def func(a):
    a = [1,2,3]+a 
    return a

a = [4,5]
print func(a)
>>> [1,2,3,4,5]
print a
>>> [4,5]

为什么会这样?在函数内部,有一个全新的命名空间(可能类似于新堆栈).所以函数内部的新a与函数外的旧a没有任何关系.

Why did this happen? Inside the function, there's a whole new namespace (which is probably something like a new stack). So the new a inside the function has nothing to do with the old a outside the function.

让python共享数据真的很难,这使得一段代码很难混淆另一部分的变量.

It's really hard to make python share data, which makes it really hard for one section of code to mess with variables in another area.

可以改变东西,但你必须跳过箍:

You can alter stuff, but you have to jump through hoops:

def func(a):
    a.append([4,5])
    return a

a = [1,2,3]
print func(a)
>>> [1,2,3,4,5]
print a
>>> [1,2,3,4,5]

看,它奏效了!我修改了a,因为我使用了a的方法,而不是创建一个新变量.

See, it worked! I altered a, because I used a method of a, not creating a new variable.

这篇关于python中对象的持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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