Christian Meesters wrote: > Hi > > I've asked a similar question before, but still I have to admit that I > didn't find a solution with this particular problem here: > > Imaging you have a class with a certain __init__ function like: > class MyClass: > def __init__(parameter1, parameter2=default,*args,**kwargs): > #handle all input > > And a member function like: > def fromFile(cls,path): > adict = {} > alist = [] > #... > #some part to read a file and to process data > > Now, how do I create an instance of MyClass when calling: x = > MyClass.fromfile(path) ? When I have a line > > return parameter1,parameter2,...,d > > in fromFile, a tuple is returned, which is not quite what I want. Is > there a way to make fromFile a true classmethod?
I think you want a static method, which does not take the class as an argument. The return value should be the new instance, not the constructor arguments. (You are expecting some magic that is not part of Python.) Try this: def fromFile(path): adict = {} alist = [] #... #some part to read a file and to process data return MyClass(parameter1,parameter2,...,d) fromFile = staticmethod(fromFile) then client code will look like this: aClass = MyClass.fromFile('a/file/path') Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor