Jan-01-2024, 07:56 AM
(This post was last modified: Jan-01-2024, 07:56 AM by Gribouillis.)
(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.
« We can solve any problem by introducing an extra level of indirection »