Python vs. Io

Jeff Epler jepler at unpythonic.net
Sat Jan 31 11:14:27 EST 2004


On Fri, Jan 30, 2004 at 05:10:16PM -0800, Daniel Ehrenberg wrote:
> There's still a difference between types and classes in Python. Try
> this in Python:
> 
> >>> x = object()
> >>> x.number = 5
> 
> It raises an error. But the same thing in Io works (which is x :=
> Object clone; x number := 5). To do it in Python, you have to do
> 
> >>> class goodObject(object): pass
> >>> x = goodObject()
> >>> x.number = 5

This is because instances of object don't have a __dict__, but instances
of goodObject do.  Says the new-style objects document:
	Instances of a class that uses __slots__ don't have a __dict__
	(unless a base class defines a __dict__); but instances of
	derived classes of it do have a __dict__, unless their class
	also uses __slots__.

object has slots=[], effectively.  The behavior is much like what you'll
see in the example below:
	class S(object): __slots__ = ['x']
	class T(S): pass

	s = S()
	s.x = 1 # succeeds
	s.y = 2 # fails with exception

	t = T()
	t.y = 3 # succeeds

Now, whether it's good to have __slots__ and __dict__ I can't tell you,
but it's this wrinkle that caused the behavior you saw, not a
"difference between types and classes".

Jeff




More information about the Python-list mailing list