473,246 Members | 1,703 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,246 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 1993
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.