亲子对象设计问题 [英] parent-child object design question

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

问题描述




我在设计课程时遇到了麻烦。


我有两节课。第一个包裹旧式类

名为oref

类CacheClass(对象):

def __init __(self,obj):

self .__ data = obj

def __getattr __(self,attr):

return getattr(self .__ data,attr)


第二类是相同的,也包装​​,但有一些

属性。


类CacheProperty(对象):

def __init __(self,obj,dicProperties = {}):

self .__ data = obj

lisProperties = []

表示key,val表示在dicProperties.iteritems():

setattr(self,key,val)

lisProperties.append(key)

self.Properties = lisProperties

def __getattr __(self,attr):

返回getattr(self .__ data,attr)


这是我的代码:

Hi,

I am having trouble designing my classes.

I have two classes. The first one wraps around an old-style class
called oref
Class CacheClass(object):
def __init__(self, obj):
self.__data = obj
def __getattr__(self, attr):
return getattr(self.__data, attr)

The second class is much the same, also wrapping, but has some
attributes.

class CacheProperty(object):
def __init__(self, obj, dicProperties={}):
self.__data = obj
lisProperties=[]
for key, val in dicProperties.iteritems():
setattr(self, key, val)
lisProperties.append(key)
self.Properties = lisProperties
def __getattr__(self, attr):
return getattr(self.__data, attr)

Here is my code:


>> MyClass = CacheClass(oref)
MyProperty = CacheProperty(oref2,{'姓名'':''姓氏'',''价值':''彼得''})
setattr(MyClass,MyProperty.Name,MyProperty)
>>MyClass = CacheClass(oref)
MyProperty = CacheProperty(oref2,{''Name'':''Surname'', ''Value'':''Peter''})
setattr(MyClass,MyProperty.Name,MyProperty)



现在,问题是我想要一个方法MyClass.MyProperty.Save()来将
保存到磁盘上的数据库后端MyClass.MyProperty的值,

但是这个代码是包装的oref代码的一部分,因此只有MyClass.set(MyProperty.Name,MyProperty.Value)调用的是
。 br />

如何在MyProperty类中访问此方法?


我希望这很清楚!


关注,s

matthew

Now, the problem is that I want a method MyClass.MyProperty.Save() to
save the value of MyClass.MyProperty to the database backend on disk,
but the code for this is part of the wrapped oref code, and thus is
invoked only by MyClass.set(MyProperty.Name,MyProperty.Value).

How can I access this method inside the MyProperty class?

I hope this is clear!

regard,s
matthew

推荐答案

" manstey" < ma ***** @ csu.edu.auwrites:
"manstey" <ma*****@csu.edu.auwrites:

我有两节课。第一个包裹旧式类

名为oref


类CacheClass(对象):

def __init __(self ,obj):

self .__ data = obj

def __getattr __(self,attr):

返回getattr(self .__ data,attr)
I have two classes. The first one wraps around an old-style class
called oref

Class CacheClass(object):
def __init__(self, obj):
self.__data = obj
def __getattr__(self, attr):
return getattr(self.__data, attr)



我假设这个类的__init__方法的''obj''参数包含

''oref''实例。


将参数调用__getattr__" attr",

有点令人困惑,因为它实际上是属性的* name *要访问,而不是

I presume the ''obj'' argument to this class''s __init__ method contains
the ''oref'' instance.

It''s a little confusing to call the argument to __getattr__ "attr",
since it''s actually the *name* of the attribute to be accessed, not
the attribute itself.


第二类是相同的,也包装​​,但有一些

属性。


类CacheProperty(对象):

def __init __(self,obj,dicProperties = {}):
The second class is much the same, also wrapping, but has some
attributes.

class CacheProperty(object):
def __init__(self, obj, dicProperties={}):



将容器类设置为默认参数容易出错;当''def''语句执行
时,

默认参数被创建一次。最好默认为None,并在

函数内触发。

Setting a container class as a default argument is prone to error; the
default argument gets created once, when the ''def'' statement is
executed. Better to default to None, and trigger on that inside the
function.


self .__ data = obj

lisProperties = []

for key,val in dicProperties.iteritems():

setattr(self,key,val)

lisProperties。 append(key)

self.Properties = lisProperties
self.__data = obj
lisProperties=[]
for key, val in dicProperties.iteritems():
setattr(self, key, val)
lisProperties.append(key)
self.Properties = lisProperties



这个类的名称并没有真正告诉我是什么的实例

class *是*。它是一个缓存属性,因为名称似乎表示?

如果是这样,为什么缓存属性呢?实例本身包含一个

属性列表?

The name of this class doesn''t really tell me what an instance of the
class *is*. Is it a "cache property", as the name seems to indicate?
If so, why does a "cache property" instance itself contain a list of
properties?


def __getattr __(self,attr):

返回getattr (自我.__数据,attr)


这是我的代码:
def __getattr__(self, attr):
return getattr(self.__data, attr)

Here is my code:

> MyClass = CacheClass(oref)
>MyClass = CacheClass(oref)



这是一个令人困惑的例子; MyClass意味着该对象是一个

类,但现在这是一个CacheClass的实例,而不是一个类。


此外,它是传统的Python在TitleCase中命名类,但是以小写字母开头的
名称实例(和函数)。

This is a confusing example; MyClass implies that the object is a
class, but this is now an instance of CacheClass, not a class.

Also, it''s conventional in Python to name classes in TitleCase, but to
name instances (and functions) starting with lowercase.


> MyProperty = CacheProperty(oref2,{''Name'':''Surname'',''Value'':''Peter''})
setattr(MyClass ,MyProperty.Name,MyProperty)
>MyProperty = CacheProperty(oref2,{''Name'':''Surname'', ''Value'':''Peter''})
setattr(MyClass,MyProperty.Name,MyProperty)



如果你不知道你打算这样做,这可能是一个很好的方法/>
将对象MyClass与MyProperty创建的
创建对象MyProperty相关联。但是:

This might be a good approach if you didn''t know you were going to
associate the object MyClass with the object MyProperty at the
creation of MyProperty. However:


现在,问题是我想要一个方法MyClass.MyProperty.Save()

来保存MyClass的值。 MyProperty到数据库后端

磁盘,但是这个代码是包装的oref代码的一部分,因此

仅由
$ b $调用b MyClass.set(MyProperty.Name,MyProperty.Value)。
Now, the problem is that I want a method MyClass.MyProperty.Save()
to save the value of MyClass.MyProperty to the database backend on
disk, but the code for this is part of the wrapped oref code, and
thus is invoked only by
MyClass.set(MyProperty.Name,MyProperty.Value).



这意味着有一个明确的每个CacheProperty实例与一个CacheClass实例相关联的
不变。


如果确实如此,最好的办法是将MyClass传递给CacheProperty类的

构造函数,并让每个实例都有设置

__init__方法中的关系。


我不知道
$ b $之间关系的描述性术语b CacheClass实例和CacheProperty实例是,所以我要去使用"'b'来使用" parent" ;;你应该选择更具描述性的东西。


类CacheProperty(对象):

def __init __(self,obj,parent,properties = None):

self .__ data = obj

self._bind_to_parent(parent)

如果属性为None:

properties = {}

self._accumulate_properties(属性)

def _bind_to_parent(self,parent):

setattr(parent,self.Name,self )


def _accumulate_properties(self,properties):

self.properties = []

表示属性中的key,val。 iteritems():

setattr(self,key,val)

self.properties.append(key)


def __getattr__ (个人,姓名):

返回getattr(自我.__数据,姓名)


-

\婚姻是一个很棒的机构,但是谁想要在一个机构中生活。

` \。 - Henry L. Mencken |
_o__)|

Ben Finney

This implies that there''s a definite "each CacheProperty instance is
associated with exactly one CacheClass instance" invariant.

If that''s true, the best thing to do is to pass MyClass to the
constructor for the CacheProperty class, and have each instance set
the relationship in the __init__ method.

I don''t know what a descriptive term for the relationship between the
CacheClass instance and the CacheProperty instance is, so I''m going to
use "parent"; you should choose something more descriptive.

class CacheProperty(object):
def __init__(self, obj, parent, properties=None):
self.__data = obj
self._bind_to_parent(parent)
if properties is None:
properties = {}
self._accumulate_properties(properties)

def _bind_to_parent(self, parent):
setattr(parent, self.Name, self)

def _accumulate_properties(self, properties):
self.properties = []
for key, val in properties.iteritems():
setattr(self, key, val)
self.properties.append(key)

def __getattr__(self, name):
return getattr(self.__data, name)

--
\ "Marriage is a wonderful institution, but who would want to |
`\ live in an institution." -- Henry L. Mencken |
_o__) |
Ben Finney


嗨Ben,


我可以做以下的事吗?

将父类存储为子类中的私有变量是什么意思?


类CacheProperty(对象):

def __init __(self,obj,parent,properties = None):

self .__ data = obj

self._parent = parent

如果属性为None:

properties = {}

self._accumulate_properties(properties)

def _accumulate_properties(self,properties):

self.properties = []

for key,val in properties.iteritems():

setattr(self,key,val)

self.properties.append(key)

def __getattr __(self,name):

return getattr(self .__ data,name)


def set(self,property):

返回self._parent.set(属性)


set函数允许我们在

孩子中调用父设定功能,这是我们需要做的事情。

Hi Ben,

Could I also do something like the following? What does it mean to
store the parent class as a private variable in the child class?

class CacheProperty(object):
def __init__(self, obj, parent, properties=None):
self.__data = obj
self._parent = parent
if properties is None:
properties = {}
self._accumulate_properties(properties)
def _accumulate_properties(self, properties):
self.properties = []
for key, val in properties.iteritems():
setattr(self, key, val)
self.properties.append(key)
def __getattr__(self, name):
return getattr(self.__data, name)

def set(self, property):
return self._parent.set(property)

the set function allows us to call the parent set function within the
child, which is what we need to do.


" manstey" < ma ***** @ csu.edu.auwrites:
"manstey" <ma*****@csu.edu.auwrites:

我可以做以下的事吗?
Could I also do something like the following?



我不能立即看到你发布的代码有问题。是不是你想要它做什么?

I can''t immediately see a problem with the code you posted. Does it do
what you want it to do?


将父类作为私有变量存储在
孩子班?
What does it mean to store the parent class as a private variable in
the child class?



我不明白这个问题。这取决于你的含义;我将b $ b留给知道该计划目的的人决定

是否父母甚至是一个合适的术语。


-

\我参加了速度等待课程。现在我可以等一个小时了......

` \只有十分钟。 - Steven Wright |

_o__)|

Ben Finney

I don''t understand this question. It''s up to you what it means; I
leave it to the people who know the purpose of the program to decide
whether "parent" is even an appropriate term.

--
\ "I took a course in speed waiting. Now I can wait an hour in |
`\ only ten minutes." -- Steven Wright |
_o__) |
Ben Finney


这篇关于亲子对象设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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