Re: [Tutor] "classmethods"
Kent Johnson wrote: > 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') A few more notes: - There is no need to make fromFile a staticmethod of MyClass, it could also be a module level function. Writing it as a staticmethod puts it in the namespace of MyClass which may be handy. - If you intend to subclass MyClass, the subclass constructors have compatible signatures and you want to be able to create subclasses with the same factory, a classmethod might work better. >>> class A(object): ... def __init__(self, val): ... self.val = val ... def show(self): ... print 'A.val:', self.val ... def factory(cls, val): ... return cls(val) ... factory = classmethod(factory) ... >>> a=A.factory(3) # calls A.factory(A, 3) >>> a.show() A.val: 3 >>> >>> class B(A): ... def show(self): ... print 'B.val:', self.val ... >>> b=B.factory(22) # calls A.factory(B, 22) >>> b.show() B.val: 22 - Finally, if you are using Python 2.4, you may prefer the decorator syntax for defining classmethods and staticmethods: class A(object): ... @classmethod def factory(cls, val): return cls(val) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "classmethods"
Hi Thanks Kent and Alan for your input. Kent's example is working like a charm. It's just that I didn't find the time to give any feedback until now. Have a nice weekend! Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "classmethods"
> Now, how do I create an instance of MyClass when calling: x = MyClass.fromfile(path) ? Why not use an instance method and just do: x = MyClass().fromFile(path) provided fromPath() returns self there should be no problem. That is, MyClass looks like: class MyClass: def __init__(self): pass def fromFile(self, fname): f = open(fname,'r') self.attribute = f.readline().strip() self.attribute1 = f.readline().strip() self.attribute2 = f.readline().strip() f.close() return self > return parameter1,parameter2,...,d Instead of returning them you store them in the object. Or am I missing something? It is possible to write true class methods but I'm not sure this is the best place to use them. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "classmethods"
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
[Tutor] "classmethods"
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? Any hint would be greatly appreciated. Regards, Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor