Two Classes In Two Files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dhable@gmail.com

    #1

    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.

  • Max M

    #2
    Re: Two Classes In Two Files

    dhable@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


    IT's Mad Science

    Phone: +45 66 11 84 94
    Mobile: +45 29 93 42 96

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: Two Classes In Two Files

      dhable@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

      Comment

      • dhable@gmail.com

        #4
        Re: Two Classes In Two Files

        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:
        dhable@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
        >

        IT's Mad Science
        >
        Phone: +45 66 11 84 94
        Mobile: +45 29 93 42 96

        Comment

        • Pedro Werneck

          #5
          Re: Two Classes In Two Files

          On 9 Aug 2006 12:35:48 -0700
          "dhable@gmail.c om" <dhable@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

          Comment

          • Gabriel Genellina

            #6
            Re: Two Classes In Two Files

            At Wednesday 9/8/2006 16:24, dhable@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:
            Long time Pythoneer Tim Peters succinctly channels the BDFL’s guiding principles for Python’s design into 20 aphorisms, only 19 of which have been written down.




            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!


            Comment

            • dhable@gmail.com

              #7
              Re: Two Classes In Two Files

              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
              "dhable@gmail.c om" <dhable@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

              Comment

              • Ant

                #8
                Re: Two Classes In Two Files


                dhable@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.

                Comment

                • Thomas Nelson

                  #9
                  Re: Two Classes In Two Files

                  Perhaps __init__.py has what you're looking for?

                  THN


                  dhable@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.

                  Comment

                  • Sion Arrowsmith

                    #10
                    Re: Two Classes In Two Files

                    Pedro Werneck <pedro.werneck@ terra.com.brwro te:
                    >"dhable@gmail. com" <dhable@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 -- siona@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

                    Comment

                    Working...