calling a function from class in python - different way

后端 未结 5 1417
别那么骄傲
别那么骄傲 2021-02-04 09:54

EDIT2: Thank you all for your help! EDIT: on adding @staticmethod, it works. However I am still wondering why i am getting a type error here.

I have just started OOPS a

相关标签:
5条回答
  • 2021-02-04 10:36

    Your methods don't refer to an object (that is, self), so you should use the @staticmethod decorator:

    class MathsOperations:
        @staticmethod
        def testAddition (x, y):
            return x + y
    
        @staticmethod
        def testMultiplication (a, b):
            return a * b
    
    0 讨论(0)
  • 2021-02-04 10:42

    You need to have an instance of a class to use its methods. Or if you don't need to access any of classes' variables (not static parameters) then you can define the method as static and it can be used even if the class isn't instantiated. Just add @staticmethod decorator to your methods.

    class MathsOperations:
        @staticmethod
        def testAddition (x, y):
            return x + y
        @staticmethod
        def testMultiplication (a, b):
            return a * b
    

    docs: http://docs.python.org/library/functions.html#staticmethod

    0 讨论(0)
  • 2021-02-04 10:47

    disclaimer: this is not a just to the point answer, it's more like a piece of advice, even if the answer can be found on the references

    IMHO: object oriented programming in Python sucks quite a lot.

    The method dispatching is not very straightforward, you need to know about bound/unbound instance/class (and static!) methods; you can have multiple inheritance and need to deal with legacy and new style classes (yours was old style) and know how the MRO works, properties...

    In brief: too complex, with lots of things happening under the hood. Let me even say, it is unpythonic, as there are many different ways to achieve the same things.

    My advice: use OOP only when it's really useful. Usually this means writing classes that implement well known protocols and integrate seamlessly with the rest of the system. Do not create lots of classes just for the sake of writing object oriented code.

    Take a good read to this pages:

    • http://docs.python.org/reference/datamodel.html
    • http://docs.python.org/tutorial/classes.html

    you'll find them quite useful.

    If you really want to learn OOP, I'd suggest starting with a more conventional language, like Java. It's not half as fun as Python, but it's more predictable.

    0 讨论(0)
  • 2021-02-04 10:59
    class MathsOperations:
        def __init__ (self, x, y):
            self.a = x
            self.b = y
        def testAddition (self):
            return (self.a + self.b)
    
        def testMultiplication (self):
            return (self.a * self.b)
    

    then

    temp = MathsOperations()
    print(temp.testAddition())
    
    0 讨论(0)
  • 2021-02-04 11:00

    you have to use self as the first parameters of a method

    in the second case you should use

    class MathOperations:
        def testAddition (self,x, y):
            return x + y
    
        def testMultiplication (self,a, b):
            return a * b
    

    and in your code you could do the following

    tmp = MathOperations
    print tmp.testAddition(2,3)
    

    if you use the class without instantiating a variable first

    print MathOperation.testAddtion(2,3)
    

    it gives you an error "TypeError: unbound method"

    if you want to do that you will need the @staticmethod decorator

    For example:

    class MathsOperations:
        @staticmethod
        def testAddition (x, y):
            return x + y
    
        @staticmethod
        def testMultiplication (a, b):
            return a * b
    

    then in your code you could use

    print MathsOperations.testAddition(2,3)
    
    0 讨论(0)
提交回复
热议问题