Two Classes In Two Files

D

dhable

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.
 
B

bearophileHUGS

(e-mail address removed):
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
 
D

dhable

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.
 
M

Max M

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
 
P

Pedro Werneck

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.
 
G

Gabriel Genellina

At said:
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
 
D

dhable

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.
 
A

Ant

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.
 
S

Sion Arrowsmith

Pedro Werneck said:
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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top