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) ?
#1
I want make a method like in the following example

1
2
3
4
5
6
7
8
9
10
11
12
13
class foo():
    
   @classmethod
   @instancemethod # problem:NameError: name 'instancemethod' is not defined
   def method(cls, self):
        print("method class =", cls)
        print("method object =", self)
 
example = foo()
example.method()
#___output___
# method class = .......
# method object = .....
how can I make it in other way ?

Thanks
Reply
#2
That doesn't really make sense. You can't call it as a class method, since you won't know the instance. Calling it as an instance method, you can get cls with type(self). So why both?

Also, in Python we almost never use class methods. Why not just a regular function?
Reply
#3
If you really want this for some reason, you could do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
#4
@micseydel,

thanks.
Reply
#5
Could you elaborate on why you need this?
Reply
#6
@micseydel,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os, sys
 
import langauge.main
 
__filepath__ = os.path.realpath(__file__)
__directory__ = os.path.dirname(__filepath__)
__progdir__ = os.path.dirname(__directory__)
 
 
class main():
    launched = False
    def __init__(self):
        pass
 
    @classmethod
    def run(cls, self):
        if cls.launched:
            pass
 
 
 
 
if __name__ == "__main__":
    program = main()
    program.run()
 
    del program.run  # this code for prevent the running again program
Reply
#7
You can achieve the same thing using the techniques shown above:

1
2
3
4
5
6
7
8
9
10
class Main(object):
 
    launched = False
 
    def run(self):
        if not self.launched:
            print('Running...')
            type(self).launched = True
        else:
            raise RuntimeError('Program has already been run.')
Output:
>>> x = Main() >>> y = Main() >>> x.run() Running... >>> y.run() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "forum_test.py", line 10, in run raise RuntimeError('Program has already been run.') RuntimeError: Program has already been run.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
@ichabod801,

Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing method attributes of python class Abedin 6 820 Apr-14-2025, 07:02 AM
Last Post: buran
  class definition and problem with a method HerrAyas 2 1,480 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 2,434 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  the order of running code in a decorator function akbarza 2 1,343 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Can I use logging in a class (without multiple messages) mevan 2 1,318 Oct-16-2023, 11:08 PM
Last Post: mevan
  Curious about decorator syntax rjdegraff42 14 4,897 May-03-2023, 01:21 PM
Last Post: rjdegraff42
  Using one child class method in another child class garynewport 5 3,210 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,928 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 4,074 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 3,647 Feb-07-2022, 07:29 PM
Last Post: saavedra29

Forum Jump:

User Panel Messages

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