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
  referencing another method in a class Skaperen 6 260 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  How to emit a signal from another Class Method? SKDN 1 279 Jun-30-2020, 05:44 PM
Last Post: Yoriz
  Use of @property decorator ruy 16 418 Jun-09-2020, 05:29 PM
Last Post: buran
  calling on a method from one class into another class which is not a child NABA 5 219 Apr-29-2020, 07:49 PM
Last Post: deanhystad
  How to make each thread send multiple requests in python3? Contra_Boy 0 131 Apr-29-2020, 02:42 PM
Last Post: Contra_Boy
  Pythonic way to handle/spread alerts class in multiple modules psolar 11 710 Feb-12-2020, 04:11 PM
Last Post: psolar
  threading for method outside class anna 0 196 Nov-17-2019, 07:05 AM
Last Post: anna
  how can I changing a base class method voidptr 2 256 Nov-10-2019, 10:53 PM
Last Post: voidptr
  How to Call a method of class having no argument dataplumber 7 825 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  Python complains that class instance is not defined colt 3 420 Sep-17-2019, 12:32 AM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)