473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

__init__.py file

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
Apr 8 '08 #1
1 4693
cesco wrote:
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.
--
Jeffrey Barish

Apr 9 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: user | last post by:
I have gotten properties to respond correctly, but when I try to do it in __init__: class foo: def getter(self): return "hello" def __init__(self):
4
by: Jan Burgy | last post by:
Hi all y'all, Consider the class down below. I've implemented it just because I needed the pushback method. Now of course the third line in __init__ doesn't work and I even understand why. My...
3
by: Gary Wilson Jr | last post by:
I'm creating a python package foo. What is intended use for __init__.py files? Well, I found this: http://www.python.org/doc/essays/packages.html >From what I can gather it is for initialization...
3
by: Daniel Schüle | last post by:
Hello Ng, I was playing around with pymedia module and I succeeded when I used complementation instead of inheritance .. but then I was forced to wrap simple methods of sound.Output like...
3
by: Sandro Dentella | last post by:
Hi everybody, I'm trying to fix the packaging of a very simple package, but some problem show me that I have not well understood the whole mechanism The structure of my package (some debug...
6
by: Rob Cowie | last post by:
Hi all, Is there a simple way to call every method of an object from its __init__()? For example, given the following class, what would I replace the comment line in __init__() with to result...
3
by: Luis P. Mendes | last post by:
Hi, I have the following problem: I instantiate class Sistema from another class. The result is the same if I import it to interactive shell. s = Sistema("par") class Sistema:
25
by: Erik Lind | last post by:
I'm new to Python, and OOP. I've read most of Mark Lutz's book and more online and can write simple modules, but I still don't get when __init__ needs to be used as opposed to creating a class...
1
by: Ryan Krauss | last post by:
I am trying to call a parent's __init__ method from the child's: class ArbitraryBlock(InnerBlock): def __init__(self, codelist, noout=False, **kwargs): InnerBlock.__init__(self, codelist,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.