Python Forum
problem usage of static method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem usage of static method
#1
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
Reply
#2
The blog's code is broken. It should be Math instead of MethodTypes.
akbarza likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
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}")
akbarza likes this post
Reply
#4
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.
Reply
#5
(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
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 267 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Static type checking with PyPy pitosalas 1 459 Dec-14-2023, 09:29 PM
Last Post: deanhystad
  Class member become static Quasar999 1 683 Sep-16-2023, 12:52 PM
Last Post: deanhystad
Photo Output Static image on HDMI2 random816382 0 1,412 Oct-18-2021, 11:21 AM
Last Post: random816382
Question Google Foobar- Code works in my IDE but not in foobar. Static method? pr3ttykitty 4 4,960 Feb-24-2021, 05:03 PM
Last Post: nilamo
  Problem with adding arbitrary parrameters to __init__ method sebastianvdn 1 1,988 Feb-03-2020, 09:30 PM
Last Post: micseydel
  Are there optimizer for static code? AlekseyPython 5 2,583 Nov-03-2019, 07:52 AM
Last Post: AlekseyPython
  problem with class method AmirAB 3 3,410 Feb-13-2019, 01:51 AM
Last Post: AmirAB
  Using static methods for library? giu88 4 2,938 Sep-12-2018, 07:52 AM
Last Post: giu88
  Notebook is rendered as static html by github miner_tom 2 3,122 Aug-23-2018, 09:00 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020