473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 ?

Jun 19 '07 #1
14 2264
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
http://geraldkaszuba.com

Jun 19 '07 #2
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
http://geraldkaszuba.com

Jun 19 '07 #3
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
http://informixdb.sourceforge.net
Jun 19 '07 #4
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 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...

http://antroy.blogspot.com/

Jun 19 '07 #5
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

Jun 19 '07 #6
Tom Gur <gu*****@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
Jun 19 '07 #7
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
Jun 19 '07 #8
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

Jun 19 '07 #9
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.

http://dirtsimple.org/2004/12/python-is-not-java.html
Jun 20 '07 #10
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
Jun 20 '07 #11
On 2007-06-19, Bjoern Schliessmann
<us**************************@spamgourmet.comwrote :
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
Jun 20 '07 #12
Neil Cerutti <ho*****@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(range(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
Jun 20 '07 #13
On 2007-06-20, Alex Martelli <al***@mac.comwrote:
Neil Cerutti <ho*****@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.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:
>>>class zap(dict): pass
...
>>>z = zap.fromkeys(range(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 misunderstanding did. ;)

--
Neil Cerutti
Ask about our plans for owning your home --sign at mortgage company
Jun 20 '07 #14
On 2007-06-20, Neil Cerutti <ho*****@yahoo.comwrote:
On 2007-06-20, Alex Martelli <al***@mac.comwrote:
>Neil Cerutti <ho*****@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
Jun 20 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael Muller | last post by:
Is there currently any plan to introduce static typing in any future version of Python? (I'm not entirely sure that "static typing" is the right term: what I'm talking about is the declaration of...
3
by: rashkatsa | last post by:
Hi all, do you know why python development team decided to forbid polymorphism for static methods ? As you can do it in another languages (Java,...) it could be very handy if you can create...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
47
by: Will Stuyvesant | last post by:
Hello all, So Zope still lives, yay. Well, I like that they use Python. <rant> What amazed me is they write that they "added types to the variables and hope that it will be added to the...
2
by: Kris | last post by:
Hi, I am a newbie to Python. With a background in Java, I was attempting to write static methods in the class without the self as the first parameter, when I got an error. I did a search for the...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
4
by: Ole Nielsby | last post by:
Here comes a generic class with a static property. Let's say they are exotic singleton pets. abstract class Pet {...} abstract class SharedPet<T>: Pet where T: SharedPet<T>, new() {...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.