设计问题:没有新属性 [英] design question: no new attributes

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

问题描述

我有一个类,其实例只应接收属性在初始化时创建的属性

赋值。

如果插槽不合适,Pythonic的设计是什么?这个?


谢谢,

Alan Isaac

I have a class whose instances should only receive attribute
assignments for attributes that were created at inititialization.
If slots are not appropriate, what is the Pythonic design for this?

Thanks,
Alan Isaac

推荐答案

Alan Isaac写道:
Alan Isaac wrote:

我有一个类,其实例只应接收属性

在初始化时创建的属性的分配。

如果插槽不合适,那么Pythonic的设计是什么?


谢谢,

Alan Isaac

I have a class whose instances should only receive attribute
assignments for attributes that were created at inititialization.
If slots are not appropriate, what is the Pythonic design for this?

Thanks,
Alan Isaac



我对Pythonic design的理解不要担心它。

如果用户想要设置不能完成任何事情的属性

高效(在你班级内)让他们。


-Larry

My understanding of "Pythonic design" is not to worry about it.
If users want to set attributes that won''t accomplish anything
productive (within your class) let them.

-Larry


" Alan Isaac" < ai **** @ american.eduwrites:
"Alan Isaac" <ai****@american.eduwrites:

我有一个类,其实例应该只接收属性

属性的赋值是在初始化时创建的。

如果插槽不合适,那么Pythonic的设计是什么?
I have a class whose instances should only receive attribute
assignments for attributes that were created at inititialization.
If slots are not appropriate, what is the Pythonic design for this?



Pythonic的设计是:不要期望对用户有这样的控制权。

你的代码。 (我在尝试设计Enum类时遇到过这种情况,

枚举成员作为属性。)


文档,在类doc字符串中以及其他任何地方感觉

合适,不应该添加属性这一事实,让你的b / b
用户如果忽略它们就会处理自己的错误。


-

\没有人破坏过低估了美国公众的口味。 - Henry L. Mencken |
_o__)|

Ben Finney

The Pythonic design is: don''t expect to have such control over users
of your code. (I encountered this in trying to design an Enum class,
with enumeration members as attributes.)

Document, in the class doc string and anywhere else you feel
appropriate, the fact that attributes shouldn''t be added, and let your
users deal with their own bugs if they ignore that.

--
\ "No one ever went broke underestimating the taste of the |
`\ American public." -- Henry L. Mencken |
_o__) |
Ben Finney


" Ben芬尼" < bi **************** @ benfinney.id.auwrote in message

news:ma ************ *************************** @ pyt hon.org ...
"Ben Finney" <bi****************@benfinney.id.auwrote in message
news:ma***************************************@pyt hon.org...

Pythonic的设计是:不要期望对用户有这样的控制权。

你的代码。
The Pythonic design is: don''t expect to have such control over users
of your code.



我知道这是一个受欢迎的回复,

但事实仍然是

它可以有帮助知道什么时候有人

试图为不存在的属性设置一个值。

如果有任何

接口,尤其如此更改。


所以我的问题仍然存在:

如何在对象初始化后最好地捕获任何此类尝试



(我愿意拥有

所有数据属性的属性,如果有帮助的话。)


谢谢,

Alan Isaac

I know this is a popular response,
but the fact of the matter remains that
it can be helpful to know when someone
tries to set a value for a nonexisting attribute.
This is especially true if there have been any
interface changes.

So my question remains:
how best to trap any such attempt
subsequent to object initialization?
(I am willing to have properties for
all data attributes, if that helps.)

Thank you,
Alan Isaac


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

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