473,326 Members | 2,076 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.

way to define static method

I think Python uses a very strange way to define static method in a
class. Why not make it like this?

class MyClass:
def my_static_method(self):
# self should be None as it's a static method
# just ignore self

I'm a newcomer so maybe it's quite naive. But I just wonder why it is
designed like this.

Thanks.
Aug 22 '07 #1
1 1145
On Wed, 22 Aug 2007 20:34:21 +0100, Eric CHAO wrote:
I think Python uses a very strange way to define static method in a
class. Why not make it like this?
What is so strange about it?
class MyClass:
def my_static_method(self):
# self should be None as it's a static method
# just ignore self

I'm a newcomer so maybe it's quite naive. But I just wonder why it is
designed like this.
That method above isn't a static method, so why should `self` be None?
Static methods are created with the `staticmethod` function which can be
used as decorator:

class MyClass(object):
@staticmethod
def my_real_static_method():
# Look ma, no `self`. :-)

Static methods are just functions, on classes `classmethod`\s are often
more useful. They get the class as first argument.

Ciao,
Marc 'BlackJack' Rintsch
Aug 22 '07 #2

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

Similar topics

0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
8
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define...
14
by: Carl Ribbegaardh | last post by:
What other c++ constructs can I use instead of #define for executing a couple of functions? Example: #define DO_STUFF doThis(); doThat(); I'd guess that I can either use a template function,...
15
by: ad | last post by:
We can define a static member in a class. But can we define a static class, so that all members are static?
8
by: ad | last post by:
I have studied buisness layer in http://beta.asp.net/QUICKSTART/util/srcview.aspx?path=~/aspnet/samples/data/GridViewObject.src It separate Author class to Author and AuthorComponent If I...
5
by: feel_energetic | last post by:
Hi, I already knew how to define a static method of a class( using staticmethod() ),but I find there isn't a built-in func to build a static field ( something like staticfield() ) can anyone...
8
by: garyusenet | last post by:
The following piece of code appears in an example i'm reading after the class has been named. public string userMessage; However if i move that into my main method, i'm told that the keyword...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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

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.