Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you pass a class (not an object) as a parameter to a method in python?

I want to do something like the following

class A:
  def static_method_A():
    print "hello"

def main(param=A):
  param.static_method_A()

I want this to be equivalent to A.static_method(). Is this possible?

like image 458
Jesse Shieh Avatar asked May 12 '09 02:05

Jesse Shieh


2 Answers

Sure. Classes are first-class objects in Python.

Although, in your example, you should use the @classmethod (class object as initial argument) or @staticmethod (no initial argument) decorator for your method.

like image 174
Chris Jester-Young Avatar answered Oct 10 '22 05:10

Chris Jester-Young


You should be able to do the following (note the @staticmethod decorator):

class A:
  @staticmethod
  def static_method_A():
    print "hello"
def main(param=A):
  param.static_method_A()
like image 22
Greg Hewgill Avatar answered Oct 10 '22 03:10

Greg Hewgill