473,326 Members | 2,173 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,326 software developers and data experts.

static class methods and data members

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 in "Learning Python" by Mark Lutz and David
Ascher. It seems like they are a relatively new feature... It seems to me that
any truly OO programming language should support these so I'm sure that Python
is no exception, but how can these be defined/implemented in Python? Currently
I have Python version 2.3 installed on my system.

Thanks,

Neil
Jul 18 '05 #1
4 7975
take a look at classmethod and staticmethod with pydoc, in the docs, or
help(). Static data members are done like so:

Class C:
iAmStatic = 1
def __init__(self):
self.iAmNotStatic = 2

Thinking of this in terms of "static" and "automatic" isn't the way to
go, really. That's C's way of doing it, and it's in Java only because
Java is nothing more than C.

Here's how it works: There are classes, and there are instances of those
classes. Each has a namespace. when one does <instance>.name, "name" is
located first in the instance namespace, then in the class namespace.

However, assignment is different. Assignment just looks in the instance
namespace, and if it's not there, it creates it. Thus if one were to do
"self.iAmStatic" in a method of C, that does not change the value of
iAmStatic for all instances of C. However, one can do "C.iAmStatic"
anywhere, and that will change "iAmStatic" for all instances of C that
do not have 'iAmStatic' bound to something at the instance level.

The best way to understand this is through experimentation in the
interactive interpreter. To make things easier, you can peek inside
python's implementation by doing C.__dict__ or anInstanceOfC.__dict__ to
see the dictionary that represents that namespace.

After you have absorbed this, you will probably see the use of
classmethod, as it's not immediately apparent if you aren't familiar with
Python. Happy hacking!

On Wed, Aug 18, 2004 at 02:42:19PM -0700, Neil Zanella wrote:
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 in "Learning Python" by Mark Lutz and David
Ascher. It seems like they are a relatively new feature... It seems to me that
any truly OO programming language should support these so I'm sure that Python
is no exception, but how can these be defined/implemented in Python? Currently
I have Python version 2.3 installed on my system.

Thanks,

Neil

Jul 18 '05 #2
Phil Frost <in****@bitglue.com> wrote in message news:<ma**************************************@pyt hon.org>...
take a look at classmethod and staticmethod with pydoc, in the docs, or
help(). Static data members are done like so:

Class C:
iAmStatic = 1
def __init__(self):
self.iAmNotStatic = 2

Thinking of this in terms of "static" and "automatic" isn't the way to
go, really. That's C's way of doing it, and it's in Java only because
Java is nothing more than C.

Here's how it works: There are classes, and there are instances of those
classes. Each has a namespace. when one does <instance>.name, "name" is
located first in the instance namespace, then in the class namespace.

However, assignment is different. Assignment just looks in the instance
namespace, and if it's not there, it creates it. Thus if one were to do
"self.iAmStatic" in a method of C, that does not change the value of
iAmStatic for all instances of C. However, one can do "C.iAmStatic"
anywhere, and that will change "iAmStatic" for all instances of C that
do not have 'iAmStatic' bound to something at the instance level.

The best way to understand this is through experimentation in the
interactive interpreter. To make things easier, you can peek inside
python's implementation by doing C.__dict__ or anInstanceOfC.__dict__ to
see the dictionary that represents that namespace.
Phil,

Your explanation is so clear, it should be included in just about any
book on Python!!! Thank you so much!!!
After you have absorbed this, you will probably see the use of
classmethod, as it's not immediately apparent if you aren't familiar with
Python. Happy hacking!


The http://www.python.org/2.2.1/descrint...#staticmethods you mentioned
really does contain some of the information I was missing pertaining to
classmethod and staticmethod. I wonder how come this file was not
installed on my Fedora Core 2 Linux distro. I also notice that
there is no corresponding file with the same name located at
http://www.python.org/doc/2.3.4/. The document seems to have
been removed from later Python documentation versions... how
come?

So, unlike Python classmethods which are roughly the equivalent
of C++ static class methods, Python staticmethods really know nothing about
the members of the class that encloses them and act pretty much like external
defs. So what's the advantage of a staticmethod over an external def?

The cls first argument to a Python classmethod is analogous to the self
first argument to __init__: when you say Class.cmethod() the Class instance
is passed as the first parameter to the class method, and when you say
instance = Class(), the instance being constructed is passed to __init__
as self, and when you say instance.imethod(), the instance is passed to
imethod() as the first parameter, self. The parameters cls and self are
barely named so by convention: they are not keywords in Python, unlike
say the this pointer which is a keyword in C++.

While qualification within a class is optional in languages like C++ and
Java, in Python you must always qualify instance variables with self within
instance methods, and always qualify class variables with cls within Python
class methods.

Since python static methods know nothing about a class, they are equivalent
to methods defined outside a class, except they may be called with the class
as the first argument (for no good reason).

Feedback, reclarification, or other,
on the above comments greatly appreciated.

Thanks,

Neil

#!/usr/bin/python

class Foo:
x = 0
y = 1
def foo(cls):
print "classmethod: hello"
print cls.x
foo = classmethod(foo)
def bar():
print "staticmethod: hello"
print Foo.x
bar = staticmethod(bar)

if __name__ == "__main__":
Foo.foo()
Foo.bar()
Jul 18 '05 #3
On 19 Aug 2004 12:23:03 -0700, Neil Zanella <nz******@cs.mun.ca> wrote:
The http://www.python.org/2.2.1/descrint...#staticmethods you mentioned
really does contain some of the information I was missing pertaining to
classmethod and staticmethod.


This file is not part of the Python documentation. If there's specific bits from
it you feel should be in the std documentation, open a SF doc bug and list
the bits that are missing.

Thanks,
Anthony
Jul 18 '05 #4

Warning: you probably don't want to bother reading all the stuff
between the "==========" lines

================================================== ======================

nz******@cs.mun.ca (Neil Zanella) writes:
So, unlike Python classmethods which are roughly the equivalent of
C++ static class methods, Python staticmethods really know nothing
about the members of the class that encloses them
And what do C++ static methods know about the class in which they are
defined? Well, they know about the class members, but have no way of
acessing any of them (other than the static ones) because there is no
this pointer. You do realize that even in C++ and Java there is a
first parameter equivalent to Python's self ... only in C++ and Java
that parameter is hidden by the surface syntax. Try looking at the
code generated by the compiler for (non-static) member functions, and
you'll see that the first parameter is the class instance ("this"),
just like in Python ... only Python doesn't lie to you about this
fact.

In C++ static methods have no "this" poniter, so they have no means of
accessing any instance attributes, because they have no knowledge of
any paricular instance. But because it is normal in C++ to access
instance attributes without explicitly specifying "this", it is
temping to be fooled that

struct foo {
int i;
static int geti() { return i; }
};

int main() {
foo f;
f.geti();
return 0;
}

might compile.

So, all you gain in C++ static methods is installing the function in
the scope of the class ..
and act pretty much like external defs.
.... just like C++ static methods.

The only practical difference between C++ static methods and Python
staticmethods is that in C++ you can access other _static_ methods (and
static data) of the class without explicitly specifying the scope in
which it is to be found:

struct foo {
static int i;
static int geti() { return i; } // this will access foo::i
};

class foo:

i = 3

def sgeti():
return foo.i # In Python must explicitly give the scope
sgeti = staticmethod(sgeti)

def cgeti(cls):
return cls.i # In Python must explicitly give the scope
cgeti = classmethod(cgeti)

================================================== ======================
So what's the advantage of a staticmethod over an external def?
It's in a namespace which the author considers to be appropriate.

Now, if you were to ask whether there is anything that can be done
with staticmethods that can's be done whith classmethods ... I'd be
hard pushed to imagine what sort of thing this could be. Anyone?

Maybe there's a minimal efficiency consideration.
The cls first argument to a Python classmethod is analogous to the self
first argument to __init__: when you say Class.cmethod() the Class instance
is passed as the first parameter to the class method,
Careful with your choice of words. The class of which the instance is
an instance (yes, the actual class itself) is passed as the first
parameter to the class method.

(There is another interpretation of the words you wrote, which is more
correct, but it's harder work to interpret your words that way :-)
and when you say instance = Class(), the instance being constructed
is passed to __init__ as self, and when you say instance.imethod(),
the instance is passed to imethod() as the first parameter,
self. The parameters cls and self are barely named so by convention:
they are not keywords in Python, unlike say the this pointer which
is a keyword in C++.

While qualification within a class is optional in languages like C++ and
Java, in Python you must always qualify instance variables with self within
instance methods, and always qualify class variables with cls within Python
class methods.
Very lucid indeed.
Since python static methods know nothing about a class, they are equivalent
to methods defined outside a class, except they may be called with the class
as the first argument (for no good reason).


Some people consider organizing things in namespaces to be an
extremely good reason. (Type "import this" into your Python REPL and
read the last line that appears.)
Jul 18 '05 #5

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

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
18
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
2
by: Vivek Ragunathan | last post by:
Hi Are the members in a static class in C# class synchronized for multiple thread access. If yes, are all static members in a C# class auto synchronized ? Regards Vivek Ragunathan
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.