这里发生了什么? [英] What's going on here?

查看:74
本文介绍了这里发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 2.4.2(#1,2006年10月13日,17:11:24)
linux2上的
[GCC 4.1.0(SUSE Linux)]

类型帮助,版权,信用和信用。或许可证或欲获得更多信息。


>> a = object()
a



< object object at 0xb7bbd438>


>> a.spam = 1



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

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

AttributeError:''object''对象没有属性''spam''


>>类b(对象):



....传递

。 ...


>> a = b()
a



< __ main __。b对象位于0xb7b4dcac>


>> a.spam = 1



在这里添加类的子类是什么?为什么我不能分配对象实例的

属性?

-

Dale Strickland-Clark

Riverhall Systems - www.riverhall.co.uk

解决方案

Dale Strickland-Clark写道:


Python 2.4.2(#1, 2006年10月13日,17:11:24)


>>> a = object()
a .spam = 1



Traceback(最近一次调用最后一次):

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

AttributeError:''object''对象没有属性''spam''


>>> B类(对象):传递
a = B()
a.spam = 1



什么是子类化添加到上课?为什么我不能分配对象实例的

属性?



对象本身没有__dict__插槽,因此可以构建非常小的对象

(例如点数)开销。


--Scott David Daniels
sc * **********@acm.org



什么是子类化添加到这堂课?



A __dict __:


>> o = object()
dir(o)



[''__class__'',' '__delattr__'',''__ doc__'',''__ getattribute__'',''__ hash__'',

''__ init _''','__ new__'',''__ reduce__'',' '__reduce_ex__'',''__repr__'',

''__ setattr__'',''_ _ ttr __''


>> class C(object):传递



.. ..


>> c = C()
dir(c )



[''__class__'',''__ delat tr__'',''__ dict__'',''__ doc__'',''__ getattribute__'',

''__ hash__'',''__ init__'',''__ module _'','' __new__'',''__ reduce__'',

''__ reduce_ex __'','_ _ _ _ _ _ _ _ _''''''''''''__ setattr _''''''_ _ _ _ _ _ _'''''''_ _ _ _ _ _''' />

-

Richie Hindle
ri **** @ entrian.com


Dale Strickland-Clark写道:


为什么我不能分配给对象实例的属性?



它没有任何属性存储。


< / F>


Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
[GCC 4.1.0 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>a = object()
a

<object object at 0xb7bbd438>

>>a.spam = 1

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

>>class b(object):

.... pass
....

>>a = b()
a

<__main__.b object at 0xb7b4dcac>

>>a.spam = 1

What is subclassing adding to the class here? Why can''t I assign to
attributes of an instance of object?
--
Dale Strickland-Clark
Riverhall Systems - www.riverhall.co.uk

解决方案

Dale Strickland-Clark wrote:

Python 2.4.2 (#1, Oct 13 2006, 17:11:24)

>>>a = object()
a.spam = 1

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

>>>class B(object): pass
a = B()
a.spam = 1


What is subclassing adding to the class here? Why can''t I assign to
attributes of an instance of object?

object itself doesn''t have a __dict__ slot, so that very small objects
(such as points) can be built without that overhead.

--Scott David Daniels
sc***********@acm.org


What is subclassing adding to the class here?

A __dict__:

>>o = object()
dir(o)

[''__class__'', ''__delattr__'', ''__doc__'', ''__getattribute__'', ''__hash__'',
''__init__'', ''__new__'', ''__reduce__'', ''__reduce_ex__'', ''__repr__'',
''__setattr__'', ''__str__'']

>>class C(object): pass

....

>>c = C()
dir(c)

[''__class__'', ''__delattr__'', ''__dict__'', ''__doc__'', ''__getattribute__'',
''__hash__'', ''__init__'', ''__module__'', ''__new__'', ''__reduce__'',
''__reduce_ex__'', ''__repr__'', ''__setattr__'', ''__str__'', ''__weakref__'']

--
Richie Hindle
ri****@entrian.com


Dale Strickland-Clark wrote:

Why can''t I assign to attributes of an instance of object?

it doesn''t have any attribute storage.

</F>


这篇关于这里发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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