473,698 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2019
dh****@gmail.co m 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.co m:
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.co m 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.c om" <dh****@gmail.c omwrote:
>
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.co m 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.c om" <dh****@gmail.c omwrote:
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.co m 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.co m 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.brwro te:
>"dh****@gmail. com" <dh****@gmail.c omwrote:
>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.gr eenend.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
8288
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 with form elements that turn out to be arrays that have to be compared with one another! I have one form element, languages, a checkbox group. Beside each checkbox is a dropdown, proficiency (which will become proficiency alongside languages)....
3
4798
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 servlet-classes I needed on the java.sun.com-Homepage and downloaded the javax.servlet-Package. (Maybe this file-description may help you understanding my problem:
9
6637
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 use the common example, I have a situation where I am implementing many types of Shapes. My current way of thinking is, well, since they are all the same type, let's just put them all in the same file. The include file would be "shapes.h" and it...
3
2561
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 lot of class declarations in the same .h, and their definitions in different .cpps? - And, to the contrary, many definitions in one cpp and declarations in different .h? - How to deal with precompiled headers? is it ok to include all headers in the...
2
9499
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 to do that? Thanks. Tsung-Yu
11
243
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 defined in .cs files somewhere and have them accessed by my ASPX, ASCX, and Code-Behind files WITHOUT having to manually compile them first???
10
2430
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 load as a 3rd partial class. This would be handy so i can generate standard code into one of the partial classes, while having my custom code untouched
11
2345
by: Sylvia A. | last post by:
How can I define global classes in web application ? Classes can be set to session variables ? Thanks
1
2792
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 been migrated to JAVA except this piece of code where C++ dll calls Forte DLL.
12
2040
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 locate the assembly "nathansokalski_com_test, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors....
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.