[Tutor] Initializing with a call like: someClass.open("someFile")?

Christian Meesters cmeesters at ucdavis.edu
Mon Feb 21 11:01:26 CET 2005


Hi

My cryptic subject is perhaps not sufficient - I'll try to make it a little better:
Assume you'd like to write something like: 

import someClass
x = someClass.open("someFile")

Here '.open' should read in the data and initialize the instance - with or without calling __init__. 
How is this to implement? Up to now I rather wrote something like x = 
someClass(somePrepFunction("someFile")) where the return value of somePrepFunction was used 
to initialize x or called the conventional 'open' within __init__. But is there no direct approach like 
in my pseudo-code?
My problem is that if I simply define open as a method of someClass (with 
def open(self,file_name): 
	#somecode
	pass
) all I get is:
TypeError: unbound method open() must be called with someClass instance as first argument 
(got str instance instead)
Which is perfectly understandable. But what is a possible workaround? (Instead of 'open' I could, 
of course, better use some other keyword which is no Python keyword.)

Any hints?

Thanks a lot in advance.
Christian


More information about the Tutor mailing list