Python Forum
problem usage of static method - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: problem usage of static method (/thread-41539.html)



problem usage of static method - akbarza - Feb-02-2024

hi
in the below code:
'''
static method
https://builtin.com/software-engineering-perspectives/python-cls
'''


class Math:
  @staticmethod
  def factorial(number):
        if number == 0:
            return 1
        else:
            return number * MethodTypes.factorial(number - 1)
       
factorial = MethodTypes.factorial(5)
print(factorial)
after running the below error is given:
Error:
Traceback (most recent call last): File "<string>", line 15, in <module> NameError: name 'MethodTypes' is not defined
the code is in the address mentioned in the docstring.
what is the problem? also why in the implementation of the function in line 13, MethodTypes is used?
thanks


RE: problem usage of static method - Gribouillis - Feb-02-2024

The blog's code is broken. It should be Math instead of MethodTypes.


RE: problem usage of static method - paul18fr - Feb-02-2024

maybe the following snippet gives you some answers:

import math

class OwnMath:
    
    @staticmethod
    def factorial(number):
      if number == 0:
          return 1
      else:
          return number * OwnMath.factorial(number - 1)


if __name__ == '__main__':
    
    n = 9
    
    factorial1 = math.factorial(n)  # using math library
    factorial2 = OwnMath.factorial(n)  # using "OwnMath" class + recursive method
    
    print(f"factorial1 with math library = {factorial1}")
    print(f"factorial2 with proper implementation= {factorial2}")



RE: problem usage of static method - deanhystad - Feb-02-2024

I went to the mentioned link and this is the first thing I saw.
Quote:The difference between the keywords self and cls reside only in the method type. If the created method is an instance method then the reserved word self to be used, but if the method is a class method then the keyword cls must be used.
This does not give a good first impression. "self" and "cls" are conventions. You should use "self" as the first argument in an instance method and "cls" is the first argument in a class method, but there is nothing in Python that says you "must" use these words.

This is followed by more misinformation:
Quote:A static method in Python must be created by decorating it with @staticmethod.
This adds a static method to the class Math
def func(number):
    result = 1
    for value in range(1, number+1):
        result *= value
    return result


class Math:
    pass

Math.factorial = func

print(Math.factorial(5))
Add on to this that the provided example of a static method contains a glaring error, and the indenting isn't even consistent. What a mess.


RE: problem usage of static method - akbarza - Feb-03-2024

(Feb-02-2024, 09:37 AM)paul18fr Wrote: maybe the following snippet gives you some answers:

import math

class OwnMath:
    
    @staticmethod
    def factorial(number):
      if number == 0:
          return 1
      else:
          return number * OwnMath.factorial(number - 1)


if __name__ == '__main__':
    
    n = 9
    
    factorial1 = math.factorial(n)  # using math library
    factorial2 = OwnMath.factorial(n)  # using "OwnMath" class + recursive method
    
    print(f"factorial1 with math library = {factorial1}")
    print(f"factorial2 with proper implementation= {factorial2}")

hi
thanks for reply
can I ask why you have used ownMath in line 10 of your code? I ask this question because factorial function has been defined in the class. I tested the cod by removing ownMath, it caused an error.
thanks again


RE: problem usage of static method - paul18fr - Feb-03-2024

You're doing the same when using classically "math.factorial" => "class.method"

Here you're creating your "OwnMath" class With your own "factorial" method (récursive use) => "OwnMath.factorial"

Do an additionnal basic test changing the method name to check you're calling your own method