is it possible to dividing up a class in multiple files?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Höfling

    is it possible to dividing up a class in multiple files?

    Hi there,

    is it possible to put the methods of a class in different files? I just
    want to order them and try to keep the files small.

    Regards
    Martin
  • Diez B. Roggisch

    #2
    Re: is it possible to dividing up a class in multiple files?

    Martin Höfling wrote:
    is it possible to put the methods of a class in different files? I just
    want to order them and try to keep the files small.
    No, its not possible. What you can do is to create several classes and one
    that inherits from all of them.

    Better yet is to not write huge classes and getting rid of strange
    conventions or views that make the problem appear as such. To explain that:
    I've never felt the need to spread a class over several files - au
    contraire, I despise Java for forcing me to only have one top level class
    per file.

    Diez

    Comment

    • Michiel Sikma

      #3
      Re: is it possible to dividing up a class in multiple files?

      Hi Martin,

      I don't think that's possible, since a file is executed when it is
      imported. If you load a file which contains a "partial" class, you
      will get an error because the indentation is incorrect, or the
      methods will be loaded in the wrong namespace.

      Regards,
      Michiel

      Op 7-aug-2006, om 15:41 heeft Martin Höfling het volgende geschreven:
      Hi there,
      >
      is it possible to put the methods of a class in different files? I
      just
      want to order them and try to keep the files small.
      >
      Regards
      Martin
      --
      http://mail.python.org/mailman/listinfo/python-list

      Comment

      • Chris Johnson

        #4
        Re: is it possible to dividing up a class in multiple files?


        Martin Höfling wrote:
        Hi there,
        >
        is it possible to put the methods of a class in different files? I just
        want to order them and try to keep the files small.
        >
        Regards
        Martin
        I ran across pyp the other day. It may be what you're wanting.



        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: is it possible to dividing up a class in multiple files?

          Martin Höfling:
          is it possible to put the methods of a class in different files? I just
          want to order them and try to keep the files small.
          Well, you can create one or more modules filled with nude methods, and
          you can define a class inside another module, and then add the methods
          to this last class using a little helper function.

          Bye,
          bearophile

          Comment

          • Ziga Seilnacht

            #6
            Re: is it possible to dividing up a class in multiple files?

            Martin Höfling wrote:
            Hi there,
            >
            is it possible to put the methods of a class in different files? I just
            want to order them and try to keep the files small.
            >
            Regards
            Martin
            You could use something like this:

            """
            Example usage:
            >>class Person(object):
            .... def __init__(self, first, last):
            .... self.first = first
            .... self.last = last
            ....
            >>john = Person('John', 'Smith')
            >>jane = Person('Jane', 'Smith')
            >>class Person(extend(P erson)):
            .... def fullname(self):
            .... return self.first + ' ' + self.last
            ....
            >>john.fullname ()
            'John Smith'
            >>jane.fullname ()
            'Jane Smith'
            """

            def extend(cls):
            extender = object.__new__( Extender)
            extender.class_ to_extend = cls
            return extender


            class Extender(object ):

            def __new__(cls, name, bases, dict):
            # check that there is only one base
            base, = bases
            extended = base.class_to_e xtend
            # names have to be equal otherwise name mangling wouldn't work
            if name != extended.__name __:
            msg = "class names are not identical: expected %r, got %r"
            raise ValueError(msg % (extended.__nam e__, name))
            # module is added automatically
            module = dict.pop('__mod ule__', None)
            if module is not None:
            modules = getattr(extende d, '__modules__', None)
            if modules is None:
            modules = extended.__modu les__ = [extended.__modu le__]
            modules.append( module)
            # replace the docstring only if it is not None
            doc = dict.pop('__doc __', None)
            if doc is not None:
            setattr(extende d, '__doc__', doc)
            # now patch the original class with all the new attributes
            for attrname, value in dict.items():
            setattr(extende d, attrname, value)
            return extended


            Ziga

            Comment

            • Bruno Desthuilliers

              #7
              Re: is it possible to dividing up a class in multiple files?

              Martin Höfling wrote:
              Hi there,
              >
              is it possible to put the methods of a class in different files? I just
              want to order them and try to keep the files small.
              Technically, yes - but in a somewhat hackish way.

              But you *really* should not have such a need at first. Smells like a
              design (or coding) problem to me - FWIW, very few of my source files are
              1 KLOC, and they usually contains many classes, functions and other
              definitions.

              Comment

              • Ant

                #8
                Re: is it possible to dividing up a class in multiple files?


                Martin Höfling wrote:
                Hi there,
                >
                is it possible to put the methods of a class in different files? I just
                want to order them and try to keep the files small.
                The editor leo (http://webpages.charter.net/edreamleo/front.html) gives
                you a way of handling large files in this way without actually having
                to split the class between files. A bit like a more powerful form of
                folding and narrowing text that some other editors have.

                But like others have noted, it is probably an indication that the class
                could use a bit of refactoring...

                Comment

                • Martin Höfling

                  #9
                  Re: is it possible to dividing up a class in multiple files?

                  Thanks for your suggestions, precompiling is not an option, cause I
                  can't introduce extra dependencies from a precompiler.
                  Better yet is to not write huge classes and getting rid of strange
                  conventions or views that make the problem appear as such. To explain that:
                  I've never felt the need to spread a class over several files - au
                  contraire, I despise Java for forcing me to only have one top level class
                  per file.
                  You're probably right. I'll think about it if it's possible to move some
                  stuff out of the class.

                  Thanks
                  Martin

                  Comment

                  • John McMonagle

                    #10
                    Re: is it possible to dividing up a class in multiple files?

                    On Mon, 2006-08-07 at 15:41 +0200, Martin Höfling wrote:
                    Hi there,
                    >
                    is it possible to put the methods of a class in different files? I just
                    want to order them and try to keep the files small.
                    >
                    Here's how I do it:

                    Firstly, I create a file called imports.py which contains all my import
                    statements. These are obviously indented at the zero position.

                    I then create a classname.py file. This just has the class statement
                    (eg: class Foo:)
                    ..

                    I then create a separate file for each function in the class. Eg:
                    init.py, function1.py, etc). These must be indented by one indent
                    position.

                    I then create a main.py file. This contains statements to be executed
                    after all the statements in the class.

                    I create a shell script which concatenates the files in the correct
                    order

                    eg:

                    cat imports.py \
                    classname.py \
                    init.py \
                    function1.py \
                    ....
                    ....
                    ...
                    main.py module.py

                    I use the concatenated file, module.py, as the program to run or import.

                    I find it easier to maintain code in this way - if I need to make a
                    change to a particular function, I just edit the file in which the
                    function is defined and re-run the shell script to make the program. If
                    I need to add a new function to the class I just need to create a new
                    file and add its entry to the shell script in the correct place and
                    re-run the shell script.

                    Of course you are not limited to a single function per file, but that is
                    what works best for me.

                    Regards,

                    John



                    --
                    This message has been scanned for viruses and
                    dangerous content by MailScanner, and is
                    believed to be clean.

                    Comment

                    • Patrick Maupin

                      #11
                      Re: is it possible to dividing up a class in multiple files?

                      Diez B. Roggisch wrote:
                      Martin Höfling wrote:
                      >
                      is it possible to put the methods of a class in different files? I just
                      want to order them and try to keep the files small.
                      >
                      No, its not possible. What you can do is to create several classes and one
                      that inherits from all of them.
                      >
                      Better yet is to not write huge classes and getting rid of strange
                      conventions or views that make the problem appear as such. To explain that:
                      I've never felt the need to spread a class over several files - au
                      contraire, I despise Java for forcing me to only have one top level class
                      per file.
                      While refactoring to avoid huge classes is usually excellent advice, it
                      actually is possible (and sometimes even useful) to merge classes
                      without using inheritance.

                      See, for example,


                      Regards,
                      Pat

                      Comment

                      • roelmathys@gmail.com

                        #12
                        Re: is it possible to dividing up a class in multiple files?


                        Martin Höfling wrote:
                        Hi there,
                        >
                        is it possible to put the methods of a class in different files? I just
                        want to order them and try to keep the files small.
                        >
                        Regards
                        Martin
                        you could do this:

                        file_1.py:

                        class A:
                        def __init__(self):
                        self.a = 1

                        file_2.py:
                        from file_1 import A

                        def seta(self,value ):
                        self.a = value

                        setattr(A,"seta ",seta)

                        but to use the "complete" class you should then import file_2 in other
                        source files.

                        bye
                        rm

                        Comment

                        Working...