Bottom Page

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
Quote
#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?
Mekire likes this post
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#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.
harun2525 likes this post
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#4
@micseydel,

thanks.
Quote
#5
Could you elaborate on why you need this?
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Quote
#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
Quote
#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.
harun2525 likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#8
@ichabod801,

Thanks.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  threading for method outside class anna 0 49 Nov-17-2019, 07:05 AM
Last Post: anna
  how can I changing a base class method voidptr 2 77 Nov-10-2019, 10:53 PM
Last Post: voidptr
  How to Call a method of class having no argument dataplumber 7 338 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  Python complains that class instance is not defined colt 3 199 Sep-17-2019, 12:32 AM
Last Post: ichabod801
  Call method from another method within a class anteboy65 3 154 Sep-11-2019, 08:40 PM
Last Post: Larz60+
  how to add class instance attributes from list 999masks 2 211 Jul-22-2019, 07:59 AM
Last Post: 999masks
  Decorator staticmethod Use Cases Devarishi 3 296 May-20-2019, 04:27 AM
Last Post: Devarishi
  How can we override decorator? bhojendra 2 283 May-12-2019, 11:15 PM
Last Post: ichabod801
  Build class to make a Smart list - trying icm63 7 417 Mar-28-2019, 08:53 PM
Last Post: icm63
  Getting error when called through instance method aankrose 2 381 Mar-02-2019, 07:19 PM
Last Post: aankrose

Forum Jump:


Users browsing this thread: 1 Guest(s)