Accessing Methods of a class using function parameter in python -
i have class in python:
class xyz: def abc(): #do def add(x,y): #add x , y def sub(x,y): #subtract y x def mul(x,y): #multiply x ynd y
note: here "add", "sub" , "mul" nested methods.
- is allowed make class this?
if allowed, want access methods "add", "sub" , "mul" function parameters. e.g.:
from xyz import xyz def jkl(input): abc.(input) # doing raise syntax error. refernece.
so, when call jkl(add(2,3)) should add numbers. when call jkl(sub(4,3)) should subtract numbers.
can done?
it's hard understand goal here.
edit: i'll leave answer here since has received upvote, op clarified they're asking how overload operators.
you can this:
class abc(): //do @staticmethod def add(x,y): # add x , y @staticmethod def sub(x,y): # subtract y x @staticmethod def mul(x,y): # multiply x ynd y abc.add(1,1)
if want use class in module, can put class definition in file xyz.py
, , in file (in same directory):
from xyz import abc abc.add(1,1)
however, odd thing in python world. i'd advise organizing things differently, unless have reason things way. better way skip class definition altogether, , in abc.py
:
def add(x,y): # add x , y def sub(x,y): # subtract y x def mul(x,y): # multiply x ynd y
then import module:
import abc abc.add(1,1) etc...
alternatively, can this:
from abc import add, sub, mul add(1,1) etc...
Comments
Post a Comment