Python Forum
@staticmethod and @classmethod
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
@staticmethod and @classmethod
#2
(Jan-01-2024, 07:30 AM)akbarza Wrote: what is the klass argument in def class_tripler(klass, x)?
The klass argument is an implicit argument which value is the calling class
>>> class A:
...     @classmethod
...     def foo(klass, x):
...         return (klass, x)
... 
>>> A.foo(3)
(<class '__main__.A'>, 3)
>>> class B(A):
...     pass
... 
>>> B.foo(4)
(<class '__main__.B'>, 4)
(Jan-01-2024, 07:30 AM)akbarza Wrote: what are the meanings of regular function and bound function?
A bound method is a small box containing an object and a function. The object is the implicit argument of the function. This bound method is callable with the rest of the arguments. In the following example, c is an instance of class C and c.spam is a bound method containing the object c and the method spam.
>>> class C:
...     def spam(self, x):
...         return (self, x)
... 
>>> c = C()
>>> c
<__main__.C object at 0x7fe88a394d90>
>>> c.spam
<bound method C.spam of <__main__.C object at 0x7fe88a394d90>>
>>> c.spam(3)
(<__main__.C object at 0x7fe88a394d90>, 3)
A bound method is very close to a partial function as returned by functools.partial(). It is an example of curried function.
akbarza likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Messages In This Thread
@staticmethod and @classmethod - by akbarza - Jan-01-2024, 07:30 AM
RE: @staticmethod and @classmethod - by Gribouillis - Jan-01-2024, 07:56 AM
RE: @staticmethod and @classmethod - by deanhystad - Jan-01-2024, 03:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Decorator staticmethod Use Cases Devarishi 3 2,788 May-20-2019, 04:27 AM
Last Post: Devarishi
  How I can recognize that member is classmethod of staticmethod? AlekseyPython 0 1,900 Feb-17-2019, 07:01 AM
Last Post: AlekseyPython
  How can i call classmethod with myClass() instead of using myClass.myClassMethod() harun2525 9 5,740 Mar-23-2017, 10:14 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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