Python Forum
how to make class and instance method (multiple decorator) ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to make class and instance method (multiple decorator) ?
#3
If you really want this for some reason, you could do something like this
class Foo():
    def __init__(self):
        self.method = self.__instance_method
    
    @classmethod
    def method(cls):
        print("method class =", cls)

    def __instance_method(self):
        print("method object =", self)
 
Foo.method()

foo = Foo()
foo.method()
Output:
method class = <class '__main__.Foo'> method object = <__main__.Foo object at 0x7f76cb5409e8>
But again, making their behavior identical doesn't really make sense.
Reply


Messages In This Thread
RE: how to make class and instance method (multiple decorator) ? - by micseydel - May-28-2017, 01:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 287 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 816 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  the order of running code in a decorator function akbarza 2 552 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Can I use logging in a class (without multiple messages) mevan 2 617 Oct-16-2023, 11:08 PM
Last Post: mevan
  Curious about decorator syntax rjdegraff42 14 2,173 May-03-2023, 01:21 PM
Last Post: rjdegraff42
  Using one child class method in another child class garynewport 5 1,655 Jan-11-2023, 06:07 PM
Last Post: garynewport
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,342 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,504 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,843 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Access instance of a class Pavel_47 5 2,118 Nov-19-2021, 10:05 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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