Michael wrote:
On 19 Dec 2010, at 21:26, Steven D'Aprano
wrote: Eike Welk wrote:
My positive attitude towards this syntax comes from the only weakness that Python IMHO has: You can't easily see which data attributes an instance has. What's wrong with dir(obj) and vars(obj)?
[...]
I think the issue that Elke is pointing out is that dir(Spam) knows nothing about y (whereas it would if you used __slots__).
Then he should have said -- he explicitly said: "You can't easily see which data attributes an INSTANCE has." [emphasis added] As a general rule, you can't expect the class to know what attributes an instance has. This is a deliberate design choice. If you want to find out what attributes an object has, you ask the object, not the object's parent class. Think of this as a variation on the Law of Demeter: if you want to know how many fleas a dog has, inspect the dog, not the dog's owner.
One answer is to have class member defaults for all instance members.
Well, that's an answer, but I'm not sure what the question is. From time to time I create a class which includes default class attributes, which then get optionally overridden by instance attributes. But that's much rarer than the usual case, where the instance has the attributes and the class doesn't. I would find it very disturbing to see classes declare meaningless class attributes (probably set to None) just to make it easier to predict what attributes the instances will get. That's pretty close to this horror: def spam(): # Declare all local variables which will be used. a = None b = None c = None # Now use them. a = 1 b = function(a, 23, -2) c = another_func(b) return a+b+c Personally, I've never found this to be a problem in practice. If I want to find out what attributes an instance of a class will have, I instantiate the class and inspect the instance. Or I read the docs. Looking at the source code is also an option (if the source code is available). For most classes, any attributes will be set in the __init__ method. If you have to read the source to determine what attributes exist, there's not much difference between: class Spam: a = 1 b = 2 c = 3 and class Spam: def __init(self): self.a = 1 self.b = 2 self.c = 3 But normally I just care about methods, not data attributes, and for that dir(cls) is perfectly adequate. (There may be the odd object that includes instance methods, but they'll be rare.) -- Steven