473,378 Members | 1,577 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,378 software developers and data experts.

Static Methods in Python

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 same on Google
and found out that there was no consistent approach to this. I would
like to know what is the prescribed approach for the same. Any
thoughts, pointers about the same would be very much appreciated.

Thanks,
Kris

Jul 19 '05 #1
2 1471
Kris wrote:
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 same on Google
and found out that there was no consistent approach to this. I would
like to know what is the prescribed approach for the same. Any
thoughts, pointers about the same would be very much appreciated.


Do something like (2.4):

@staticmethod
def aStaticMethod(x, y, z):
...

Prior to 2.4, use:

def aStaticMethod(x, y, z):
...
aStaticMethod = staticmethod(aStaticMethod)

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Wyrd has swept all my kin / all the brave chiefs away! / Now I must
follow them! -- Beowulf
Jul 19 '05 #2

"Kris" <ia*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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 same on Google
and found out that there was no consistent approach to this. I would
like to know what is the prescribed approach for the same. Any
thoughts, pointers about the same would be very much appreciated.
Generally, there's very little need for static methods in Python. That
niche is better handled by module level functions. You can use real
static methods with new style classes, using the staticmethod built
in function. However, you can't reference class variables with
static methods - you should use class methods to do that. See the
Python library documentation.

John Roth
Thanks,
Kris


Jul 19 '05 #3

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

Similar topics

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...
3
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
3
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.