1

I want to pass a class name to a method as a constant.

Example:

def foo(Bar): # ... 

Eventually, I want to be able to use Bar as a class name in method foo

Is this possible to do?

2
  • Usually there is no need to pass a constant as an argument to a function. Commented Oct 26, 2016 at 10:50
  • What you mean by "pass a class name to a method as a constant" ? And also by "use Bar as a class name in method foo" ? Commented Oct 26, 2016 at 10:59

2 Answers 2

1

Yes, you can pass class as an argument to function. Below is the example to prove it.

class Bar(): @staticmethod # created staticmethod; can be call without creating the object def my_print(): print 'Hello, I am class function' def foo(my_class): my_class().my_print() # call my_print() of the object passed foo(Bar) # Pass class "Bar" as argument to "foo()" # prints: Hello, I am class function 
Sign up to request clarification or add additional context in comments.

Comments

0

Check out this

class Bar(): pass def foo(param): print "class_name is {}".format(param.__name__) #prints class name foo(Bar) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.