类“对象”的实例 [英] Instance of class "object"
问题描述
我好,
我想知道为什么以下不起作用。
a = object()
ab = 1#dynamic绑定属性失败...
为了使其正确,我们必须创建一个新类:
class MyClass(object):pass
a = MyClass()
ab = 1#OK
这种奇怪的行为会破坏LSP(Liskov替换原则)吗?
问候,
-
ShenLei
" ??????" < li ************** @ gmail.comwrites:
你好,
我想知道以下为什么不起作用。
a = object()
ab = 1#动态绑定属性失败...
为了使其正确,我们必须创建一个新类:
class MyClass(object):pass
a = MyClass()
ab = 1#OK
搜索此信息非常困难; python
类对象类型继承绑定属性并不足以让我们知道我们正在谈论内置的''对象''
类型,并继承它。
我很想知道你问题的答案;谢谢你的询问。
-
\不要害怕失去机会。每次失败的背后都有一个| b $ b'\是一个人们希望他们错过的机会。 - 简|
_o__)Wagner,来自Lily Tomlin |
Ben Finney
" ??? ???" < li ************** @ gmail.comwrites:
你好,
我想知道为什么下面不起作用。
a = object()
ab = 1#动态绑定属性失败...
因为默认对象类没有dict或其他
状态指示。它是一个纯粹的 Python对象,其唯一可见的
属性是其类型及其标识。 (在实现
级别它也有一个refcount。)
这是必要的,因为所有其他Python对象(都是新式的
类和C扩展名)继承自''对象'',在C级别。
在''对象'中有状态意味着在* all * other
Python对象。另一方面,当前的设计允许
创建非常轻量级的python对象,甚至没有
dict。
子类化对象指示Python你想要什么样的状态
你的班级。默认是有一个dict来存储
属性:
#带有dict的类 - 任何属性都通过dict,并创建一个
#Foo对象实际上创建了两个对象,一个Foo和一个dict
class Foo(对象):
pass
但你也可以指定:
#一个高效的''对''类,持有两个对象
class Pair(object):
__slots__ =''first'',''second''
对的实例占用更少的2元素元组空间
因为他们没有在对象中携带大小信息。
现在,如果对象类带有一个dict,它将是
不可能创建像''Pair''这样的类。
为了使它正确,我们必须创建一个新类:
class MyClass(对象):传递
a = MyClass()
ab = 1#OK
是否他的奇怪行为打破了LSP(Liskov替换
原则)?
从LSP开始,子类可能不会引入新的
前置条件。在这种情况下,子类接受一个
原始类没有的情况,所以它没有引入新的前提条件,它
只是弱化(删除)现有的。
??????写道:
我想知道为什么以下不起作用。
a = object()
ab = 1#动态绑定属性失败...
插槽的实现取决于该行为:
http://docs.python.org/ref/slots.html
< blockquote class =post_quotes>
这种奇怪的行为会破坏LSP(Liskov替换原则)吗?
你可以扩展吗?
Peter
Howdy,
I wonder why below does not work.
a = object()
a.b = 1 # dynamic bind attribute failed...
To make it correct, we have to create a new class:
class MyClass(object): pass
a = MyClass()
a.b = 1 # OK
Does this strange behavior break the LSP (Liskov substitution principle)?
Regards,
--
ShenLei
"??????" <li**************@gmail.comwrites:
Howdy,
I wonder why below does not work.
a = object()
a.b = 1 # dynamic bind attribute failed...
To make it correct, we have to create a new class:
class MyClass(object): pass
a = MyClass()
a.b = 1 # OKIt''s annoyingly difficult to search for information on this; "python
class object type inherit bind attribute" aren''t sufficiently
distinguishing to know that we''re talking about the built-in ''object''
type, and inheriting from it.
I''m interested to know the answer to your question; thanks for asking it.
--
\ "Don''t be afraid of missing opportunities. Behind every failure |
`\ is an opportunity somebody wishes they had missed." -- Jane |
_o__) Wagner, via Lily Tomlin |
Ben Finney
"??????" <li**************@gmail.comwrites:
Howdy,
I wonder why below does not work.
a = object()
a.b = 1 # dynamic bind attribute failed...Because the default object class doesn''t have a dict or other
indication of state. It''s a "pure" Python object whose only visible
properties are its type and its identity. (On the implementation
level it also has a refcount.)
This is necessary because all other Python objects (both new-style
classes and C extensions) inherit from ''object'', on the C level.
Having state in ''object'' would mean having that same in *all* other
Python objects. The current design, on the other hand, allows
creation of very lightweight python objects that don''t even have a
dict.
Subclassing object instructs Python as to what kind of state you want
your class to have. The default is to have a dict to store
properties:
# a class with dict -- any property goes through dict, and creating a
# Foo object actually creates two objects, one Foo and one dict
class Foo(object):
pass
But you can also specify:
# an efficient ''Pair'' class holding two objects
class Pair(object):
__slots__ = ''first'', ''second''
Instances of Pair take up even less room that 2-element tuples
because they don''t carry the size information in the object.
Now, if the object class carried a dict with it, it would be
impossible to create a class like ''Pair''.
To make it correct, we have to create a new class:
class MyClass(object): pass
a = MyClass()
a.b = 1 # OK
Does this strange behavior break the LSP (Liskov substitution
principle)?It follows from LSP that what the subclass may not introduce new
preconditions. In this case the subclass accepts a case that the
original class didn''t, so it doesn''t introduce a new precondition, it
simply weakens (removes) an existing one.
?????? wrote:
I wonder why below does not work.
a = object()
a.b = 1 # dynamic bind attribute failed...The implementation of slots depends on that behaviour:
http://docs.python.org/ref/slots.html
Does this strange behavior break the LSP (Liskov substitution principle)?Can you expand on that?
Peter
这篇关于类“对象”的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!