473,386 Members | 1,753 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,386 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 7982
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.