Answers for "class and static methods in python"

0

python call static method in class

# Python methods have a __func__ attribute which, when called
# on a staticmethod, allows it to be executed within a class
# body.
class MyClass:
  @staticmethod
  def static_method(n: int) -> int:
    return n
  
  num = static_method.__func__(10) # Call __func__ with argument

mc = MyClass()
print(mc.num) # Output: 10
Posted by: Guest on April-21-2021
1

instance method in python

# Instance Method Example in Python 
class Student:
    
    def __init__(self, a, b):
        self.a = a
        self.b = b 
    
    def avg(self):
        return (self.a + self.b) / 2

s1 = Student(10, 20)
print( s1.avg() )
Posted by: Guest on August-12-2020

Code answers related to "class and static methods in python"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language