By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

What's going on here?

P: n/a
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

Nov 22 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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
Nov 22 '06 #2

P: n/a
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
Nov 22 '06 #3

P: n/a
Dale Strickland-Clark wrote:
Why can't I assign to attributes of an instance of object?
it doesn't have any attribute storage.

</F>

Nov 22 '06 #4

P: n/a
Perhaps this piece of code might explain the behaviour:

>>class C( object ):
.... __slots__ = ()
....
>>o = C()
o.a = 1
Traceback (most recent call last):
File "<input>", line 1, in ?
AttributeError: 'C' object has no attribute 'a'
object behaves like having an implict __slots__ attribute.
HTH,
Gerald
Dale Strickland-Clark schrieb:
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?
Nov 22 '06 #5

P: n/a
Dale Strickland-Clark wrote:
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?

Python sooooo dynamic, but it lacks a (builtin) X-class ready for ad-hoc usage just like dict() :-)
I have in almost every app/toolcore-module this one:

------

class X(object):
def __init__(self,_d={},**kwargs):
kwargs.update(_d)
self.__dict__=kwargs
class Y(X):
def __repr__(self):
return '<Y:%s>'%self.__dict__

------

x=X(spam=1)

Maybe X should be renamed to __builtin__.Object ...
Robert
Nov 22 '06 #6

P: n/a

robert wrote:
Dale Strickland-Clark wrote:
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?


Python sooooo dynamic, but it lacks a (builtin) X-class ready for ad-hoc usage just like dict() :-)
I have in almost every app/toolcore-module this one:

------

class X(object):
def __init__(self,_d={},**kwargs):
kwargs.update(_d)
self.__dict__=kwargs
class Y(X):
def __repr__(self):
return '<Y:%s>'%self.__dict__

------

x=X(spam=1)

Maybe X should be renamed to __builtin__.Object ...

Have you considered putting it in one file and *importing* it into
"almost every app/toolcore-module"?

Have you considered that others may like to have something a little
more elaborate, like maybe using the pprint module, or that the amount
of data that would spew out might in some cases be so great that they
wouldn't want that every time from repr(), preferring a dump-style
method that wrote to a logfile?

IMHO that's one of the enormous number of good points about Python; you
can easily lash up something like that to suit yourself and inject it
into any class you like; there's no central authority tying your hands
behind your back.

HTH,
John

Nov 22 '06 #7

P: n/a
John Machin wrote:
robert wrote:
>Dale Strickland-Clark wrote:
>>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?

Python sooooo dynamic, but it lacks a (builtin) X-class ready for ad-hoc usage just like dict() :-)
I have in almost every app/toolcore-module this one:

------

class X(object):
def __init__(self,_d={},**kwargs):
kwargs.update(_d)
self.__dict__=kwargs
class Y(X):
def __repr__(self):
return '<Y:%s>'%self.__dict__

------

x=X(spam=1)

Maybe X should be renamed to __builtin__.Object ...


Have you considered putting it in one file and *importing* it into
"almost every app/toolcore-module"?
(yes its in my core python "language extension" module, which I import frequently in apps)
Have you considered that others may like to have something a little
more elaborate, like maybe using the pprint module, or that the amount
of data that would spew out might in some cases be so great that they
wouldn't want that every time from repr(), preferring a dump-style
method that wrote to a logfile?
(in X is no repr so far. of course one could make a default repr with short output. had no frequent needs so far)
IMHO that's one of the enormous number of good points about Python; you
can easily lash up something like that to suit yourself and inject it
into any class you like; there's no central authority tying your hands
behind your back.
its more about the general case, trying things out on the interactive etc. always - thus when I want speed not "suit" :-)

very often I need a dummy object and find me always typing "class X:pass" or import above tools.
Think this trivial but needed Object() thing is possibly more than a private sitecustomize-thing. Thats why I wrote here upon seeing others trying object() which doesn't do what one expects at first.
It wouldn't really tie hands or ? but possibly converse

Robert
Nov 23 '06 #8

P: n/a
Thanks for the answers. I am informed but I don't feel enlightened.

It does strike me as odd that an apparently empty subclass should add extra
function to the base class.

Not at all obvious.
--
Dale Strickland-Clark
We are recruiting Python programmers. Please see the web site.
Riverhall Systems www.riverhall.co.uk

Nov 23 '06 #9

P: n/a
Dale Strickland-Clark wrote:
Thanks for the answers. I am informed but I don't feel enlightened.

It does strike me as odd that an apparently empty subclass should add extra
function to the base class.

Not at all obvious.
Yes. As said, there is missing a __builtin__.Object

object is not an "empty class", but the base builtin-type:
>>isinstance(int,object)
True

built-in type instances are basically read-only because if ...

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

...would work, that would be very strange.
Maybe in a mud place language like Ruby, where you can jam and scribble everywhere in the system and in builtins, such things are possible.
I'd propose to add something trivial like
class Object(object):
def __init__(self,_d={},**kwargs):
kwargs.update(_d)
self.__dict__=kwargs
...

to Python. I use such empty (common) class since all time I can think of - and frequently.
If there would be a common such Empty class in Python you'd have a lot of advantanges. From ad-hoc instances, "OO-dicts" to reliable pickling of bunches of variables etc.
Robert
Nov 23 '06 #10

P: n/a
Dale Strickland-Clark wrote:
Thanks for the answers. I am informed but I don't feel enlightened.

It does strike me as odd that an apparently empty subclass should add extra
function to the base class.

Not at all obvious.
Remember that a class definition is syntax sugar for a direct call to
type:

type(name,bases,clsdict)

This constructs and returns the class. It's free to add behaviors even
if the clsdict is empty, and that's exactly what it does in this case.
type considers the absence of __slots__ in clsdict to be a request for
an instance dict, and so it adds one. (Types defined in C aren't
created that way, however. I believe C-defined types request an
instance dict by reserving space for it in the object structure and
setting tp_dictoffset to indicate its offset.)

This is not obvious, but it's really the only reasonable way. You
can't have the base class of all objects creating dicts for its
instances--you wouldn't want every int object to have it's own dict.
And you can't have Python class instances not have a dict by default.
Carl Banks

Nov 24 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.