473,405 Members | 2,262 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,405 software developers and data experts.

Two Classes In Two Files

I just started working with Python and ran into an annoyance. Is there
a way to avoid having to use the "from xxx import yyy" syntax from
files in the same directory? I'm sure it's been asked a million times,
but I can't seem to find the answer.

For example, I have two classes stored in separate files as such.

File: one.py
========
class One:
def methodA(self):
print "class One"
def methodB(self):
print "class One"
File two.py
========
from one import One

class Two(One):
def methodA(self):
print "class Two"

if __name__ == "__main__":
x = Two()
x.methodA()
x.methodB()

When I run the Two.py file, I get the expected output but I'd like to
eliminate the from line in two.py.

Aug 9 '06 #1
9 2002
dh****@gmail.com wrote:
I just started working with Python and ran into an annoyance. Is there
a way to avoid having to use the "from xxx import yyy" syntax from
files in the same directory? I'm sure it's been asked a million times,
but I can't seem to find the answer.
Probably none that are better.

1:
import one
class Two(one.One)

2:
put both classes in the same file.
It's just the way it is. Why worry about it?

For example, I have two classes stored in separate files as such.

File: one.py
========
class One:
def methodA(self):
print "class One"
def methodB(self):
print "class One"
File two.py
========
from one import One

class Two(One):
def methodA(self):
print "class Two"

if __name__ == "__main__":
x = Two()
x.methodA()
x.methodB()

When I run the Two.py file, I get the expected output but I'd like to
eliminate the from line in two.py.

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Phone: +45 66 11 84 94
Mobile: +45 29 93 42 96
Aug 9 '06 #2
dh****@gmail.com:
Is there
a way to avoid having to use the "from xxx import yyy" syntax from
files in the same directory?
You can just use:
import xxx

and then:
class Two(xxx.One):
...

If you don't want to use the import line, you have to put the two
classes into the same module.

Bye,
bearophile

Aug 9 '06 #3
It's just the way it is. Why worry about it?

Wasn't so much a worry, just trying to figure out how to think the
python way.

Max M wrote:
dh****@gmail.com wrote:
I just started working with Python and ran into an annoyance. Is there
a way to avoid having to use the "from xxx import yyy" syntax from
files in the same directory? I'm sure it's been asked a million times,
but I can't seem to find the answer.

Probably none that are better.

1:
import one
class Two(one.One)

2:
put both classes in the same file.
It's just the way it is. Why worry about it?

For example, I have two classes stored in separate files as such.

File: one.py
========
class One:
def methodA(self):
print "class One"
def methodB(self):
print "class One"
File two.py
========
from one import One

class Two(One):
def methodA(self):
print "class Two"

if __name__ == "__main__":
x = Two()
x.methodA()
x.methodB()

When I run the Two.py file, I get the expected output but I'd like to
eliminate the from line in two.py.


--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Phone: +45 66 11 84 94
Mobile: +45 29 93 42 96
Aug 9 '06 #4
On 9 Aug 2006 12:35:48 -0700
"dh****@gmail.com" <dh****@gmail.comwrote:
>
It's just the way it is. Why worry about it?

Wasn't so much a worry, just trying to figure out how to think the
python way.
Seems like you're thinking the Java way... if you don't want to do it,
put both classes in the same file.

--
Pedro Werneck
Aug 9 '06 #5
At Wednesday 9/8/2006 16:24, dh****@gmail.com wrote:
>I just started working with Python and ran into an annoyance. Is there
a way to avoid having to use the "from xxx import yyy" syntax from
files in the same directory? I'm sure it's been asked a million times,
but I can't seem to find the answer. [...]
When I run the Two.py file, I get the expected output but I'd like to
eliminate the from line in two.py.
Embody the Zen of Python:
http://www.python.org/dev/peps/pep-0020/

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Aug 10 '06 #6
Yes, I have been ruined for the last 5 years with Java and C#. Perl was
my only salvation, but now I can't read the programs I wrote.

Pedro Werneck wrote:
On 9 Aug 2006 12:35:48 -0700
"dh****@gmail.com" <dh****@gmail.comwrote:
It's just the way it is. Why worry about it?
Wasn't so much a worry, just trying to figure out how to think the
python way.

Seems like you're thinking the Java way... if you don't want to do it,
put both classes in the same file.

--
Pedro Werneck
Aug 10 '06 #7
Ant

dh****@gmail.com wrote:
Yes, I have been ruined for the last 5 years with Java and C#. Perl was
my only salvation, but now I can't read the programs I wrote.
ROFL! That's got to be a contender for Quote of the week.

Aug 10 '06 #8
Perhaps __init__.py has what you're looking for?

THN
dh****@gmail.com wrote:
I just started working with Python and ran into an annoyance. Is there
a way to avoid having to use the "from xxx import yyy" syntax from
files in the same directory? I'm sure it's been asked a million times,
but I can't seem to find the answer.

For example, I have two classes stored in separate files as such.

File: one.py
========
class One:
def methodA(self):
print "class One"
def methodB(self):
print "class One"
File two.py
========
from one import One

class Two(One):
def methodA(self):
print "class Two"

if __name__ == "__main__":
x = Two()
x.methodA()
x.methodB()

When I run the Two.py file, I get the expected output but I'd like to
eliminate the from line in two.py.
Aug 10 '06 #9
Pedro Werneck <pe***********@terra.com.brwrote:
>"dh****@gmail.com" <dh****@gmail.comwrote:
>Wasn't so much a worry, just trying to figure out how to think the
python way.
Seems like you're thinking the Java way... if you don't want to do it,
put both classes in the same file.
OP: think of a .py file as being more akin to Java package than a .java
file. Don't worry about the resulting files getting too large -- Python
is a lot more compact than Java.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Aug 10 '06 #10

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

Similar topics

17
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now...
3
by: TheLetti | last post by:
Hy! I've just downloaded the latest Java 2 SDK (j2sdk1.4.2_01). For my surprise in this version the servlet-classes are not integrated (e.g. the class javax.servlet). So I found all the...
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
3
by: Javi | last post by:
I have some doubts about what is the best method to distribute classes in .cpp and .h files: - Should I use a file per class? or should I group similar classes in one file? - Is it good to put a...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
11
by: Alex Maghen | last post by:
I love that I can create ASPX and ASCX files with CS code embedded or in code-behind files and ASP.NET will auto-compile it all for me. I *like* it that way Is there ANY way to have classes...
10
by: ptass | last post by:
Hi In asp.net 2.0 an aspx files .cs file is a partial class and all works fine, however, I thought I’d be able to create another class file, call it a partial class and have that compile and...
11
by: Sylvia A. | last post by:
How can I define global classes in web application ? Classes can be set to session variables ? Thanks
1
by: Swapnil Kale | last post by:
Hi, I'm working on a Migration project (Forte to JAVA). The forte client had a C++ dll which used to call one more FORTE dll for a complex database calculations. Now all the forte code has...
12
by: Nathan Sokalski | last post by:
I have several CustomControls that I have written for my project. However, when I try to compile I recieve the following warning & errors: Warning 32 Could not resolve this reference. Could not...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.