473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

What's going on here?

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
10 1144
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
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
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
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
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Tony Marston | last post by:
Several months ago I started a thread with the title "What is/is not considered to be good OO programming" which started a long and interesting discussion. I have condensed the arguments into a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
9
by: andrew | last post by:
Hi, I posted this message recently, but received no response, so I figured I'd try again. I can't find anybody who can tell me what's going on here. I'm having a problem with fieldsets in IE...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
8
by: Midnight Java Junkie | last post by:
Dear Colleagues: I feel that the dumbest questions are those that are never asked. I have been given the opportunity to get into .NET. Our organization has a subscription with Microsoft that...
63
by: Jake Barnes | last post by:
In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: http://www.developer-x.com/content/innerhtml/default.html The case is made that innerHTML should never...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
6
by: LurfysMa | last post by:
I am working on an electronic flashcard application. I have one version up and running using Visual Basic (6.0) and Access 2000. My long-range plans are to put it up on a website and sell...
10
by: JoeC | last post by:
I have been programming for a while and I have seen this syntax before and I copied this from a book but the book didn't explain what is going on here. class engine{ protected: static engine*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.