实例行为 [英] Instances behaviour

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

问题描述

大家好,

我已经使用Python 3年了,但我很少使用它的OOP

功能(我是物理学家,抱歉)。现在,在阅读了很多有关Python OOP功能的内容之后,我正试图利用这一点(对我来说)

新的范例。结果我有很多有些哲学上的问题。我将从其中一个开始。


假设我有一堆类代表稍微(但概念上是
)不同的对象。每个类的实例必须以非常相似的方式表现为
,这样我就创建了一个共同的类祖先

(比如说A)定义了很多特殊的方法(例如__getattr __,

__setattr __,__ len__等),然后我创建了所有继承自它的真实

类:

Hi all,
I''ve been using Python for 3 years, but I''ve rarely used its OOP
features (I''m a physicist, sorry). Now, after having read a lot about
Python OOP capabilities, I''m trying to get advantage of this (for me)
new paradigm. As a result I''ve a lot of somewhat philosophical
questions. I will start with one of them.

Suppose I have a bunch of classes that represent slightly (but
conceptually) different object. The instances of each class must behave
in very similar manner, so that I''ve created a common class ancestor
(let say A) that define a lot of special method (such as __getattr__,
__setattr__, __len__ and so on), and then I''ve created all my "real"
classes inheriting from it:

A类(对象):
.....#here定义所有特殊方法和一些常用方法

B级(A):
.....#这是第一个真实的 class

class C(A):
class A(object): ..... # here define all special and some common methods
class B(A): ..... # this is the first "real" class
class C(A):



.....#这是第二个


等等。我担心的问题是,一个不知情的用户可能会创建一个A的实例。假设它有任何实际用途,而它b $ b只是一种原型。但是,我无法看到(从我有限的

的观点来看)任何其他重新安排事物的方式,仍然会得到类似的行为。


直接在B类中实现这些特殊方法,然后从它继承

,看起来不是正确的方法,因为我更喜欢

B与C的实例没有任何关系(即我不愿意将B $从B子类化为



也许一些OOP技术(我现在想念)可以是任何

帮助。有什么建议吗?


提前致谢,

Andrea。


..... # and this is the second

and so on. The problem I''m worried about is that an unaware user may
create an instance of "A" supposing that it has any real use, while it
is only a sort of prototype. However, I can''t see (from my limited
point of view) any other way to rearrange things and still get a
similar behaviour.

Implementing those special methods directly in class B and then inherit
from it, doesn''t seem the right way, since I''d prefer that instances of
B weren''t in any relation with the instances of C (i.e. I''d prefer not
to subclass C from B)

Perhaps some OOP techniques (that I miss at the moment) could be of any
help. Any suggestion?

Thanks in advance,
Andrea.

推荐答案

2005年12月1日星期四03:51:05 -0800,Mr.Rech写道:

[...]
On Thu, Dec 01, 2005 at 03:51:05PM -0800, Mr.Rech wrote:
[...]
假设我有一个一堆代表略微(但概念上)不同对象的类。每个类的实例必须以非常相似的方式表现,以便我创建了一个共同的类祖先(比如说A)定义了很多特殊方法(例如__getattr __,
__setattr __,__ len__等等,然后我创建了所有继承自它的真实类:
Suppose I have a bunch of classes that represent slightly (but
conceptually) different object. The instances of each class must behave
in very similar manner, so that I''ve created a common class ancestor
(let say A) that define a lot of special method (such as __getattr__,
__setattr__, __len__ and so on), and then I''ve created all my "real"
classes inheriting from it:
A类(对象):....#here定义所有特殊方法和一些常用方法
B类(A):....#这是第一个真实方法上课类别C类(A):....#这是第二个

等等。我担心的问题是,不知情的用户可能会创建A的实例。假设它有任何实际用途,而它只是一种原型。但是,我无法(从我有限的观点)看到任何其他方式来重新排列事物并仍然得到类似的行为。
class A(object): .... # here define all special and some common methods
class B(A): .... # this is the first "real" class
class C(A): .... # and this is the second

and so on. The problem I''m worried about is that an unaware user may
create an instance of "A" supposing that it has any real use, while it
is only a sort of prototype. However, I can''t see (from my limited
point of view) any other way to rearrange things and still get a
similar behaviour.


A类(对象):
... def __init __(self,foo):
...如果self .__ class__是A:
...引发TypeError(" A是基类。)
... self.foo = foo
...
B类(A):
....传递

.... C类(A):
.... def __init __(自我,foo,酒吧):

.... A .__ init __(self, foo)

.... self.bar = bar

.... a = A(1)
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件"< stdin>",第4行,在__init__

TypeError:A是基类。 b = B(1)
b.foo
1 c = C(1,2)
c.foo,c.bar
(1,2)
class A(object):
... def __init__(self, foo):
... if self.__class__ is A:
... raise TypeError("A is base class.")
... self.foo = foo
...
class B(A): .... pass
.... class C(A): .... def __init__(self, foo, bar):
.... A.__init__(self, foo)
.... self.bar = bar
.... a = A(1) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in __init__
TypeError: A is base class. b = B(1)
b.foo 1 c = C(1, 2)
c.foo, c.bar (1, 2)




HTH

- Inyeol Lee



HTH
--Inyeol Lee


" Mr.Rech" <一个************* @ gmail.com>写道:
"Mr.Rech" <an*************@gmail.com> writes:
假设我有一堆类代表略微(但概念上)不同的对象。每个类的实例必须以非常相似的方式表现,以便我创建了一个共同的类祖先(比如说A)定义了很多特殊方法(例如__getattr __,
__setattr __,__ len__等),然后我创建了所有继承自它的真正的类:

等等。我担心的问题是,不知情的用户可能会创建A的实例。假设它有任何实际用途,而它只是一种原型。但是,我不能(从我有限的观点)看到任何其他方式来重新安排事情并仍然得到类似的行为。

也许是一些OOP技术(我想念的那一刻)可以是任何帮助。有什么建议吗?
Suppose I have a bunch of classes that represent slightly (but
conceptually) different object. The instances of each class must behave
in very similar manner, so that I''ve created a common class ancestor
(let say A) that define a lot of special method (such as __getattr__,
__setattr__, __len__ and so on), and then I''ve created all my "real"
classes inheriting from it:

and so on. The problem I''m worried about is that an unaware user may
create an instance of "A" supposing that it has any real use, while it
is only a sort of prototype. However, I can''t see (from my limited
point of view) any other way to rearrange things and still get a
similar behaviour.

Perhaps some OOP techniques (that I miss at the moment) could be of any
help. Any suggestion?




我假设有B& B的方法C不是共享的,因此

不在A.当用户调用它们时,它们应该收到错误

消息。这就是通常处理这种事情的方式。


如果你想在实例化时间发生事情,那么你可以制作

A. __init__引发异常。你的B& C __init__然后就不能调用

了。如果A .__ init__有实际用途,请将其转换为另一种方法B

& C'的__init__可以调用。


< mike

-

Mike Meyer< mw * @ mired。有机> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件了解更多信息。



I assume there are methods of B & C that aren''t shared, and hence
aren''t in A. When the user invokes those, they should get an error
message. That''s how this kind of thing is normally dealt with.

If you want things to happen at instantiation time, then you can make
A.__init__ raise an exception. Your B & C __init__ then can''t invoke
it. If A.__init__ has a real use, move that into another method that B
& C''s __init__ can invoke.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


Mr.Rech写道:
Mr.Rech wrote:
假设我有一堆类略代表(但在概念上)不同的对象。每个类的实例必须以非常相似的方式表现,以便我创建了一个共同的类祖先(比如说A)定义了很多特殊方法(例如__getattr __,
__setattr __,__ len__等等,然后我创建了所有继承自它的真实类:
Suppose I have a bunch of classes that represent slightly (but
conceptually) different object. The instances of each class must behave
in very similar manner, so that I''ve created a common class ancestor
(let say A) that define a lot of special method (such as __getattr__,
__setattr__, __len__ and so on), and then I''ve created all my "real"
classes inheriting from it:
A类(对象):.... *****#* here * define * all * special *和* some * common * methods
class B(A):.... ** **#* this *是* * first *" real" * class
class C(A):.... ****#* and * this * is * the * second

等等。我担心的问题是,不知情的用户可能会创建A的实例。假设它有任何实际用途,而它只是一种原型。但是,我无法(从我有限的观点)看到任何其他方式重新排列事物并仍然得到类似的行为。

直接实现这些特殊方法在B级然后从它继承
,看起来不是正确的方式,因为我更喜欢B的实例与C的实例没有任何关系(即我不愿意从B中继承C语言。

也许一些OOP技术(我现在想念的)可以是任何帮助。有什么建议吗?
class A(object): ....*****#*here*define*all*special*and*some*common *methods
class B(A): ....****#*this*is*the*first*"real"*class
class C(A): ....****#*and*this*is*the*second

and so on. The problem I''m worried about is that an unaware user may
create an instance of "A" supposing that it has any real use, while it
is only a sort of prototype. However, I can''t see (from my limited
point of view) any other way to rearrange things and still get a
similar behaviour.

Implementing those special methods directly in class B and then inherit
from it, doesn''t seem the right way, since I''d prefer that instances of
B weren''t in any relation with the instances of C (i.e. I''d prefer not
to subclass C from B)

Perhaps some OOP techniques (that I miss at the moment) could be of any
help. Any suggestion?




怎么样


A类(对象):

"" ;为类似A的类提供通用功能,例如: G。 B和C.


不要实例化。

"""


这绝对是一个低技术的方法,但我想你也不会用虚假的论证类型检查弄乱你的b $ b函数。

像Inyeol Lee所示的异常是通常在开发过程中提出一次

,而我的方法只会(仅)

文盲程序员咬一下,如



How about

class A(object):
"""Provides common functionality for A-like classes, e. g. B and C.

Do not instantiate.
"""

This is definitely a low-tech approach, but I suppose you don''t clutter your
functions with spurious argument type checks, either.
An exception like the one shown by Inyeol Lee is typically raised once
during the development process while my approach bites (only) the
illiterate programmer with a message like

a .foo()



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

AttributeError:'''''对象没有属性''foo''


通常几乎可以快速跟踪 - 无论如何,它为他服务

:-)


彼得


Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: ''A'' object has no attribute ''foo''

which normally can be tracked down almost as quickly -- and which serves him
well anyway :-)

Peter


这篇关于实例行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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