static python classes ?

T

Tom Gur

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

google

the python equivalent of what's called in most OOP languages "static
Look for @staticmethod inhttp://docs.python.org/lib/built-in-funcs.html

Woops... I misread...
 
C

Carsten Haese

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

Ant

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 B(object):
.... def aaa(self):
.... print "AAAAAA"
....AAAAAA

(In contrast the equivalent of Java's ordinary inner class:.... def __init__(self):
.... class B(object):
.... def aa(self):
.... print "BBBB"
.... self.B = B
....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.
 
T

Tom Gur

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
 
B

Ben Finney

Tom Gur said:
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?
 
D

Diez B. Roggisch

Tom said:
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
 
B

Bjoern Schliessmann

Diez said:
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
 
B

Bruno Desthuilliers

Tom Gur a écrit :
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.

http://dirtsimple.org/2004/12/python-is-not-java.html
 
D

Diez B. Roggisch

Bjoern said:
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
 
N

Neil Cerutti

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

Alex Martelli

Neil Cerutti said:
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:

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
 
N

Neil Cerutti

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.today') 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:


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 misunderstanding did. ;)
 
N

Neil Cerutti

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.
 

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

Forum statistics

Threads
474,269
Messages
2,571,097
Members
48,773
Latest member
Kaybee

Latest Threads

Top