__init__.py file

C

cesco

Hi,

I need to instantiate an object (my_object) whose methods I have to
use in two files (file1.py and file2.py) which are in the same
directory. Is it possible to instantiate such object in the
__init__.py file and then directly use it in file1.py and file2.py?
If not, as I seem to experience, what is the best practice to follow
in this case? (I thought __init__.py was somehow useful for that).

Many thanks
Francesco
 
S

Steven W. Orr

On Tuesday, Apr 8th 2008 at 16:51 -0000, quoth cesco:

=>Hi,
=>
=>I need to instantiate an object (my_object) whose methods I have to
=>use in two files (file1.py and file2.py) which are in the same
=>directory. Is it possible to instantiate such object in the
=>__init__.py file and then directly use it in file1.py and file2.py?
=>If not, as I seem to experience, what is the best practice to follow
=>in this case? (I thought __init__.py was somehow useful for that).

Sounds more like a job for a singleton. I recently found one that I like a
lot better than the standard recipe:

class Singleton(object):
__single = None # the one, true Singleton

def __new__(classtype, *args, **kwargs):
if classtype != type(classtype.__single):
classtype.__single = object.__new__(classtype, *args, **kwargs)
return classtype.__single

def __init__(self,name=None):
"""Arg doesn't have to be str."""
self.name = name

def display(self):
print self.name,id(self),type(self)

The advantage of this one is that it can be nicely subclassed.

if __name__ == "__main__":

class SubSingleton(Singleton):
def __init__(self, name=None):
Singleton.__init__(self, name)
self.aa = 123
self.bb = 456
self.cc = 789

o1 = Singleton('foo')
o1.display()
o2 = Singleton('bar')
o2.display()
o3 = SubSingleton('foobar')
o3.display()
o4 = SubSingleton('barfoo')
o4.display()
print 'o1 = o2:',o1 == o2
print 'o1 = o3:',o1 == o3
print 'o3 = o4:',o3 == o4

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
 
G

Gabriel Genellina

I need to instantiate an object (my_object) whose methods I have to
use in two files (file1.py and file2.py) which are in the same
directory. Is it possible to instantiate such object in the
__init__.py file and then directly use it in file1.py and file2.py?
If not, as I seem to experience, what is the best practice to follow
in this case? (I thought __init__.py was somehow useful for that).

Yes, you can, but consider passing the object as an argument. Global
objects aren't necesarily evil, anyway I don't have enough information to
make an opinion.

If you insist on use the global object, there is another question.
__init__.py defines the package namespace, and is the public interfase to
it, I would not put an object there that is not supposed to be part of the
package public interfase. You can instantiate the object in any other
module, and import such module from both file1 and file2.
 
J

Jeffrey Barish

cesco said:
I need to instantiate an object (my_object) whose methods I have to
use in two files (file1.py and file2.py) which are in the same
directory. Is it possible to instantiate such object in the
__init__.py file and then directly use it in file1.py and file2.py?
If not, as I seem to experience, what is the best practice to follow
in this case? (I thought __init__.py was somehow useful for that).

I do this and find the technique fantastically convenient. You don't
explain what problems you encountered -- or perhaps I don't understand what
you mean by using the instances "directly" -- but here's what I do: In
dirA/dirB/__init__.py, I import my_object to import the my_object.py
module. I then instantiate an object in __init__.py. (Note that it is a
singleton because Python imports modules only once.) Import the object in
file{1,2}.py with import dirA.dirB.my_object or from dirA.dirB import
my_object. I use the technique only with objects that are legitimately
global.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top