Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python staticmethod() Function

Python staticmethod() Function

Python built-in function Python built-in function

Python staticmethod returns a static method of a function.

This method does not require parameters to be passed, as in the following declaration of a static method:

class C(object):
    @staticmethod
    def f(arg1, arg2, ...):
        ...

The above example declares a static method f, so it can be used through an instance as C().f(). Of course, it can also be called without instantiating as C.f().

Function syntax


staticmethod(function)

Parameter Description:

Example

#!/usr/bin/python

-- coding: UTF-8 --

class

C

(

object

)

: @

staticmethod

def

f

(

)

:

print

(

'

runoob

'

)

;

C

.

f

(

)

;

A static method needs no instantiation

cobj

=

C

(

)

cobj

.

f

(

)

It can also be called after instantiation

The output of the above example is:

runoob
runoob

Python built-in function Python built-in function