Instance of class "object"
castironpi
castironpi at gmail.com
Sat May 17 06:19:59 EDT 2008
On May 17, 1:09 am, Carl Banks <pavlovevide... at gmail.com> wrote:
> On May 16, 4:46 am, "甜瓜" <littlesweetme... at gmail.com> wrote:
>
> > Howdy,
> > I wonder why below does not work.
>
> > a = object()
> > a.b = 1 # dynamic bind attribute failed...
>
> > To make it correct, we have to create a new class:
> > class MyClass(object): pass
> > a = MyClass()
> > a.b = 1 # OK
>
> > Does this strange behavior break the LSP (Liskov substitution principle)?
>
> I suspect it's possible to come up with a property that causes LSP to
> be violated for any two given distinct classes.
>
> For example, if the property you're considering is "Raises an
> exception when you try to access an attribute", then yes, the subtype
> cannot be substituted for the base type and would violate LSP.
> However, that's not really such a useful property in most cases.
>
> The reason instances of object don't have dynamic attributes is
> because dyanmic attributes are internally represented by a dict
> object. To keep memory footprint low, instances of many built-in
> types don't have an associated dict and so can't bind attributes
> dynamically. Instances of object can't have an associated dict
> because, if they did, it would mean all objects would have to have an
> associated dict, because all types are subclasses of object.
>
> Because of this, direct instances of object really don't have much use
> in Python. It's one main use case is as a sentinel or marker of some
> sort. It's only useful property is that it is itself, and nothing
> else. But, that property is true of all instances in Python;
> therefore any instance may be substituted for an object instances,
> therefore it satisfies LSP.
>
> (Phew: what a tangle of nomenclature that was.)
>
> Carl Banks
Python is the same language on sufficiently many distinct machines
that all the people can use it if they want. That means identical
outputs. Do their 100%-ers go with the times?
More information about the Python-list
mailing list