Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python call function in class

#------------------------------------
#CLASS
#------------------------------------
class Student:
  def __init__(self):
    self.name = None
    
  def set_name(self, word):
    self.name = word
    return self.get_name()
    
  def get_name(self):
    return self.name
  
#------------------------------------
# USAGE:
#------------------------------------

a = Student()
print(a.set_name("Hello"))
Comment

call class function by string python

import foo
method_to_call = getattr(foo, 'bar')
result = method_to_call()
Comment

how to run class.function from name python

class A:
    def __init__(self):
        pass

    def sampleFunc(self, arg):
        print('you called sampleFunc({})'.format(arg))

m = globals()['A']()
func = getattr(m, 'sampleFunc')
func('sample arg')

# Sample, all on one line
getattr(globals()['A'](), 'sampleFunc')('sample arg')
Comment

call class function by string python

result = getattr(foo, 'bar')()
Comment

PREVIOUS NEXT
Code Example
Python :: python loop backwards 
Python :: Check np.nan value 
Python :: lists to dictionary python 
Python :: concatenation array 
Python :: how to use assert in python 
Python :: python list last element 
Python :: save image to database using pillow django 
Python :: self in python 
Python :: default values python 
Python :: get char from ascii value python 
Python :: word2vec 
Python :: assertionerror-accepted-renderer-not-set-on-response-in-django 
Python :: pronic number program in python 
Python :: try catch python with open 
Python :: axios django post data 
Python :: adfuller test in python 
Python :: extract coordinate values in xarray 
Python :: install scrapy on pycharm 
Python :: a int and float. python 
Python :: python decorator 
Python :: copy dataframe columns names 
Python :: python convert b string to dict 
Python :: how to check if a variable holds a class reference in python 
Python :: python enumerate 
Python :: flask app with spark 
Python :: function in the input function python 
Python :: python boolean operators 
Python :: how to take space separated input in pyhon dicationary 
Python :: keyboard python 
Python :: fizz buzz 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =