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

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
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,

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:

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
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  the order of running code in a decorator function akbarza 2 476 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Can I use logging in a class (without multiple messages) mevan 2 534 Oct-16-2023, 11:08 PM
Last Post: mevan
  Curious about decorator syntax rjdegraff42 14 1,971 May-03-2023, 01:21 PM
Last Post: rjdegraff42
  Using one child class method in another child class garynewport 5 1,483 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,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,379 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,678 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Access instance of a class Pavel_47 5 2,032 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,656 Oct-30-2021, 11:20 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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