Files

26 lines
1.1 KiB
Python
Raw Permalink Normal View History

2023-04-06 17:03:21 -07:00
class C:
@classmethod
def class_method(cls):
print("Called class_method with cls:", cls)
def instance_method(self):
print("Called instance_method with self:", self)
@staticmethod
def static_method():
print("Called static_method")
# Accessing class attributes
class_method_obj = C.__dict__["class_method"]
instance_method_obj = C.__dict__["instance_method"]
static_method_obj = C.__dict__["static_method"]
# Calling the methods directly (without transformation)
class_method_obj.__func__(C) # Output: Called class_method with cls: <class '__main__.C'>
instance_method_obj.__func__(1) # Output: Called instance_method with self: <__main__.C object at 0x7f8d7c1e4d90>
static_method_obj.__func__() # Output: Called static_method
# Accessing the methods through class attribute reference (with transformation)
C.class_method() # Output: Called class_method with cls: <class '__main__.C'>
C.instance_method(1) # Raises TypeError: instance_method() missing 1 required positional argument: 'self'
C.static_method() # Output: Called static_method