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 -- http://mail.python.org/mailman/listinfo/python-list