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

2005-02-22 Thread Christian Meesters

Thanks Guys,

Guess I have to look once more into decorator functions - and guess it's worth 
doing so. Anyway: 
My problem is solved.
Cheers
Christian
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2005-02-21 Thread Christian Meesters

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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2005-02-21 Thread Ryan Davis
You could make the __init__ take the filename:

def __init__(self, filename=None):
   #do whatever

And then instantiate the object like:
x = someClass(filename=someFile)

Alternatively, you could make the Open function a module level function, 
defined in someClass.py, but not in the actual class
definition.

There's also a way to do it with @staticmethod, but I haven't ever touched that.

Thanks,
Ryan 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Christian 
Meesters
Sent: Monday, February 21, 2005 5:01 AM
To: tutor@python.org
Subject: [Tutor] Initializing with a call like: someClass.open(someFile)?


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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor