Re: Can we create an_object = object() and add attribute like for a class?



Alex Martelli wrote:

Pierre Rouleau <prouleau@xxxxxxxxxxxxxxxxxx> wrote:


Hi all,

Is there any reason that under Python you cannot instantiate the object
class and create any attributes like you would be able for a normal class?


Yep: instances of type object do not have a __dict__ and therefore there
is no place to put any attributes. This is necessary to allow ANY
subclass of object, and thus any type whatsoever, to lack a per-instance
__dict__ (and thus to save its per-instance memory costs),

Makes sense. I was under the impresssion that instances of type object did have a __dict__ but was hidden for some reason. I should have known .... explicit is better than implicit...


Being able to do it would seem a natural way of declaring namespaces.


I find that ns = type('somename', (), dict(anattribute=23)) isn't too
bad to make a namespace ns, though it has some undesirable issues (e.g.,
ns is implicitly callable, which may make little sense for a namespace).

At any rate, any natural way of declaring a namespace SHOULD allow
arbitrary named arguments in the instantiation call -- bending
principles to give each instance of object a __dict__ would still not
fix that, so that wouldn't do much. I think it's worth the minor bother
to write out something like

class Namespace(object):
def __init__(self, **kwds): self.__dict__ = kwds

and I generally go further anyway, by defining at least a repr that
shows the attributes' names and values (very useful for debugging...).


That's what I do too, but sometimes i just want to have a quick holder attribute in a class and am lazy to do it. I could declare one such class inside a module and import it but again, that means importing, name space of the module, etc...

I can understand the design decision not to give object a __dict__, but I wonder if i'd be a good idea to have a class that derives from object and has a __dict__ to be in the standard library. I posted the original question because I run into this quite often and I just saw a post a little before mine ("self modifying code") where the idiom was used.

--

Pierre Rouleau
.



Relevant Pages

  • Re: Can we create an_object = object() and add attribute like for a class?
    ... Is there any reason that under Python you cannot instantiate the object ... which may make little sense for a namespace). ...
    (comp.lang.python)
  • Re: CORRECTED TEST AND ANSWERS.
    ... > Write code example to instantiate that class. ... What you explain as a bad programming practice is, in reality, a programming ... > Public, Friend and Private. ... > Private In the declaring class/module ...
    (microsoft.public.vb.general.discussion)
  • Re: Declare an object as its parent class?
    ... > Is there any reason to declare an object as its Parent or Grandparent ... then instantiate it as the intended class? ... then instantiate as its own object. ...
    (comp.lang.java.programmer)
  • Re: ProgID in .NET?
    ... The ProgID has the form Project.ClassName.Version. ... version it is possible to instantiate COM objects from different versions ... When a class from a .NET assembly is instantiated its namespace and class ... The nice part about COM is that you can choose as a developer if you want to use a specific version of a COM object or not without the vendor of the DLL facing the problem we ran now into: If we don't change the namespace you can't use different versions in the same process. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Is there a limit to how many table you can have in a dataset?
    ... tables and I recognize that it does take longer to instantiate a dataset in ... is the insanely complex code and super insanely complex ... >> is there any good reason why having 50 or so tables in a dataset schema ...
    (microsoft.public.dotnet.framework.adonet)