Second attempt, my PC crashed just as I was sending the first one. You might see two similar posts, if so apologies...
On 06/01/16 14:46, Steven D'Aprano wrote: > I don't understand what you mean by "Python doesn't support named > constructors". It seems to me that this is the *only* sort of > constructor that Python supports. No, Python constructors have names(new/init) but they are not used explicitly (ie. by the client code)to create objects. By contrast languages like Delphi, Smalltalk, Objective C, some Lisps and, I think, Eiffel (and others?) all require an explicit call of a constructor method and there may be multiple constructors per class each with different names (usually describing how the construction occurs or the nature of the object constructed). For example in Smalltalk: obj := MyClass new. # new is the normal choice of name obj2 := Date today. obj3 := Date new. obj4 := Time now. All of these methods (new, today, now) are constructors(*) but with explicit names that tells you something about the kind of instance created. (*)As I understand it, Smalltalk constructors are just class methods allocated to a constructor category. But the category is just an organisational device used by the IDE/library it doesn't actually change the code in any way. > As I understand it, "named constructor" comes from the C++ world, where > functions are matched not just by name, but by parameters as well. That's overloading which uses the same name but with different numbers/types of parameters and/or return type. The problem with that is that in C++ constructors must have the name of the class. So if you want multiple constructors that all take a single string as their paramater then it gets tricky. > two integers representing the position relative to the entire screen, or > two integers representing the position relative to the current window. > The C++ compiler cannot distinguish those two cases, and would give an > error. Exactly so. > The solution is to use constructors with different names, which C++ > calls "named constructors". I stopped using C++ around v2 and it didn't have such a feature. Maybe its been added since. If so that's good to know. (Time for some googling methinks...) > So the only way to have two different constructors is to give them > different names. > > Hence all constructors in Python are "named constructors". But they are not native constructors such as those used in Smalltalk etc. They have to be factory methods that call new/init under the covers. And that's how most OOP languages that don't support named constructors get round it. (Although interestingly, Objective C refers to all constructors as 'factory methods', even the 'default' ones.) > It would be reasonable to make this a factory function declared in the > global module level. But I think making it a classmethod is better. I won't put up much of an argument here. In C++ or Java I'd definitely say use a class method. But it seems to me that I've seen more factory functions in modules than class method factories. But that's not been a detailed analysis just a gut feel for what seems idiomatic in Python. It also seems easier for beginners to grasp than the more esoteric notion of class methods. > ... ensures that if you subclass the class, you automatically > get a valid constructor as well. Wouldn't it return an instance of the superclass rather than the sub class? You'd need to override it wouldn't you? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor