Tuesday, September 15, 2009

Python - create static methods in a class

#create static methods in a python class

import math

class MyStaticClass:
def getSqrt(num):
return math.sqrt(num)
def getHalf(num):
return num/2
# labels the methods as static
getSqrt = staticmethod(getSqrt)
getHalf = staticmethod(getHalf)

MyStaticClass.getSqrt(144)
12.0
MyStaticClass.getHalf(20)
10

No comments:

Post a Comment