static python classes ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Gur

    #1

    static python classes ?

    Hi,

    I'm new to python, and I can't seem to find in the docs how to create
    the python equivalent of what's called in most OOP languages "static
    classes", can you give me a hint ?

  • google@gakman.com

    #2
    Re: static python classes ?

    On Jun 19, 10:00 pm, Tom Gur <gur....@gmail. comwrote:
    Hi,
    >
    I'm new to python, and I can't seem to find in the docs how to create
    the python equivalent of what's called in most OOP languages "static
    classes", can you give me a hint ?
    Look for @staticmethod in http://docs.python.org/lib/built-in-funcs.html

    Example:
    class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

    --
    Gerald Kaszuba


    Comment

    • google@gakman.com

      #3
      Re: static python classes ?

      the python equivalent of what's called in most OOP languages "static
      classes", can you give me a hint ?
      >
      Look for @staticmethod inhttp://docs.python.org/lib/built-in-funcs.html
      Woops... I misread...

      --
      Gerald Kaszuba


      Comment

      • Carsten Haese

        #4
        Re: static python classes ?

        On Tue, 2007-06-19 at 12:00 +0000, Tom Gur wrote:
        Hi,
        >
        I'm new to python, and I can't seem to find in the docs how to create
        the python equivalent of what's called in most OOP languages "static
        classes", can you give me a hint ?
        If I had to guess, which apparently I have to because you're not telling
        us what *you* think a "static class" is, I'd say you want a class where
        all methods are static. In Python, there is no point in making such a
        thing. Just make a module that defines the functions you need.

        If this doesn't help, please explain to us what you're actually trying
        to achieve.

        --
        Carsten Haese



        Comment

        • Ant

          #5
          Re: static python classes ?

          It's not clear what you mean here. If you mean something like static
          inner classes in Java, then you can simply nest classes in Python:
          >>class A(object):
          .... class B(object):
          .... def aaa(self):
          .... print "AAAAAA"
          ....
          >>z = A.B()
          >>z.aaa()
          AAAAAA

          (In contrast the equivalent of Java's ordinary inner class:
          >>class A(object):
          .... def __init__(self):
          .... class B(object):
          .... def aa(self):
          .... print "BBBB"
          .... self.B = B
          ....
          >>A().B().aa( )
          BBBB

          )

          If you mean "static class" as in an class which only has static
          methods and variables, then the python equivalent is a module.
          I'm new to python, and I can't seem to find in the docs how to create
          the python equivalent of what's called in most OOP languages "static
          classes", can you give me a hint ?
          Hope the above was what you are looking for.

          --
          Ant...

          Notes on programming, cycling, climbing, linux and stuff.


          Comment

          • Tom Gur

            #6
            Re: static python classes ?

            Look for @staticmethod inhttp://docs.python.org/lib/built-in-funcs.html
            >
            Example:
            class C:
            @staticmethod
            def f(arg1, arg2, ...): ...

            Oops, sorry for the confusion - I've actually meant a static method,
            and Gerald's answer works fine.
            Thanks alot

            Comment

            • Ben Finney

              #7
              Re: static python classes ?

              Tom Gur <gur.tom@gmail. comwrites:
              I'm new to python, and I can't seem to find in the docs how to
              create the python equivalent of what's called in most OOP languages
              "static classes", can you give me a hint ?
              Can you give us a hint of what a "static class" would do? That is,
              what features do you require that you can't find how to implement?

              --
              \ "I've always wanted to be somebody, but I see now that I should |
              `\ have been more specific." -- Jane Wagner, via Lily Tomlin |
              _o__) |
              Ben Finney

              Comment

              • Diez B. Roggisch

                #8
                Re: static python classes ?

                Tom Gur wrote:
                Hi,
                >
                I'm new to python, and I can't seem to find in the docs how to create
                the python equivalent of what's called in most OOP languages "static
                classes", can you give me a hint ?
                With other OOP languages you mean Java. Which does have static methods
                because they lack the notion of a function by its own, so the shoehorned
                them into their "everything is inside a class"-paradigm.

                Forget about that. As well as getters and setters, interfaces and the like.

                Just use a module-level function.

                diez


                Comment

                • Bjoern Schliessmann

                  #9
                  Re: static python classes ?

                  Diez B. Roggisch wrote:
                  With other OOP languages you mean Java. Which does have static
                  methods because they lack the notion of a function by its own, so
                  the shoehorned them into their "everything is inside a
                  class"-paradigm.
                  ACK, but doesn't C++ have static methods too?

                  Regards,


                  Björn

                  --
                  BOFH excuse #236:

                  Fanout dropping voltage too much, try cutting some of those little
                  traces

                  Comment

                  • Bruno Desthuilliers

                    #10
                    Re: static python classes ?

                    Tom Gur a écrit :
                    >Look for @staticmethod inhttp://docs.python.org/lib/built-in-funcs.html
                    >>
                    >Example:
                    >class C:
                    > @staticmethod
                    > def f(arg1, arg2, ...): ...
                    >
                    >
                    Oops, sorry for the confusion - I've actually meant a static method,
                    and Gerald's answer works fine.
                    FWIW, staticmethods in Python are of very restricted use - we have
                    modules and functions for this. And you may be interested in
                    classmethods (methods that takes the class object instead of the
                    instance as first argument).

                    As a last word : trying to write Java in Python wis certainly not the
                    best option. Better to learn to write Python.


                    Comment

                    • Diez B. Roggisch

                      #11
                      Re: static python classes ?

                      Bjoern Schliessmann wrote:
                      Diez B. Roggisch wrote:
                      >
                      >With other OOP languages you mean Java. Which does have static
                      >methods because they lack the notion of a function by its own, so
                      >the shoehorned them into their "everything is inside a
                      >class"-paradigm.
                      >
                      ACK, but doesn't C++ have static methods too?
                      Might be, I'm a bit rusty on C++. But they do have "normal" functions as
                      well - so I figured the OP wouldn't have troubles then.

                      Diez

                      Comment

                      • Neil Cerutti

                        #12
                        Re: static python classes ?

                        On 2007-06-19, Bjoern Schliessmann
                        <usenet-mail-0306.20.chr0n0s s@spamgourmet.c omwrote:
                        Diez B. Roggisch wrote:
                        >With other OOP languages you mean Java. Which does have static
                        >methods because they lack the notion of a function by its own,
                        >so the shoehorned them into their "everything is inside a
                        >class"-paradigm.
                        >
                        ACK, but doesn't C++ have static methods too?
                        C++ does have what it calls "static member functions".

                        class foo
                        {
                        public:
                        static void bar() { /* nada */ }
                        };

                        Python names a similar thing a @staticmethod (the name taken from
                        C++, obviously, since C++ originated the essentially meaningless
                        word-grouping "static member function" in order to reuse an
                        existing keyword).

                        // It may be called with two different syntaxes, as in Python:

                        void goo()
                        {
                        // ...using the qualified name:
                        foo::bar();
                        // ...or with an instance of the class:
                        foo f;
                        f.bar();
                        }

                        In C++ they are used most often for factory functions, since they
                        conveniently have access to the class's private members, and
                        don't want or need an existing instance. Python seems to have
                        adopted this use-case (ConfigParser, for example), but without a
                        need for it (code organization?).

                        --
                        Neil Cerutti
                        I am free of all prejudices. I hate everyone equally. --W. C. Fields

                        Comment

                        • Alex Martelli

                          #13
                          Re: static python classes ?

                          Neil Cerutti <horpner@yahoo. comwrote:
                          In C++ they are used most often for factory functions, since they
                          conveniently have access to the class's private members, and
                          don't want or need an existing instance. Python seems to have
                          adopted this use-case (ConfigParser, for example), but without a
                          need for it (code organization?).
                          What staticmethod does ConfigParser have? Can't recall one offhand.

                          I think Python more commonly uses classmethod, rather than staticmethod,
                          for factories (i.e. a la Smalltalk, not a la C++). In that case the
                          advantage wrt a function _is_ there: when you subclass, you can get
                          instances of the new class, rather than the base class, from the
                          factory:
                          >>class zap(dict): pass
                          ....
                          >>z = zap.fromkeys(ra nge(4))
                          >>z
                          {0: None, 1: None, 2: None, 3: None}
                          >>type(z)
                          <class '__main__.zap'>
                          >>>
                          If dict_fromkeys was a plain function (or if fromkeys was a staticmethod
                          rather than a classmethod of dict) then z would be an instance of dict,
                          rather than one of zap (unless you also specifically passed the desired
                          class, kind of cumbersome).


                          Alex

                          Comment

                          • Neil Cerutti

                            #14
                            Re: static python classes ?

                            On 2007-06-20, Alex Martelli <aleax@mac.comw rote:
                            Neil Cerutti <horpner@yahoo. comwrote:
                            >
                            >In C++ they are used most often for factory functions, since they
                            >conveniently have access to the class's private members, and
                            >don't want or need an existing instance. Python seems to have
                            >adopted this use-case (ConfigParser, for example), but without a
                            >need for it (code organization?).
                            >
                            What staticmethod does ConfigParser have? Can't recall one
                            offhand.
                            I misremembered which module I had recently seen it in: it was
                            datetime, not ConfigParser. And ('datetime.toda y') is, as you
                            point out, a class method rather than a static method.

                            Thanks for the correction.
                            I think Python more commonly uses classmethod, rather than
                            staticmethod, for factories (i.e. a la Smalltalk, not a la
                            C++). In that case the advantage wrt a function _is_ there:
                            when you subclass, you can get instances of the new class,
                            rather than the base class, from the factory:
                            >
                            >>>class zap(dict): pass
                            ...
                            >>>z = zap.fromkeys(ra nge(4))
                            >>>z
                            {0: None, 1: None, 2: None, 3: None}
                            >>>type(z)
                            ><class '__main__.zap'>
                            >>>>
                            >
                            If dict_fromkeys was a plain function (or if fromkeys was a
                            staticmethod rather than a classmethod of dict) then z would be
                            an instance of dict, rather than one of zap (unless you also
                            specifically passed the desired class, kind of cumbersome).
                            Thanks. That makes much more sense than my misunderstandin g did. ;)

                            --
                            Neil Cerutti
                            Ask about our plans for owning your home --sign at mortgage company

                            Comment

                            • Neil Cerutti

                              #15
                              Re: static python classes ?

                              On 2007-06-20, Neil Cerutti <horpner@yahoo. comwrote:
                              On 2007-06-20, Alex Martelli <aleax@mac.comw rote:
                              >Neil Cerutti <horpner@yahoo. comwrote:
                              >>In C++ they are used most often for factory functions, since
                              >>they conveniently have access to the class's private members,
                              >>and don't want or need an existing instance. Python seems to
                              >>have adopted this use-case (ConfigParser, for example), but
                              >>without a need for it (code organization?).
                              >>
                              >What staticmethod does ConfigParser have? Can't recall one
                              >offhand.
                              >
                              I misremembered which module I had recently seen it in:
                              A quick search of the 2.5 Lib directory had the following
                              results. Only sqlite3 uses the staticmethod built-in function for
                              a family of conversion functions.

                              The other references to staticmethod were either tests of the
                              involving them, or introspection facilities that had to deal with
                              them.

                              --
                              Neil Cerutti

                              Comment

                              Working...