Python Forum
How can i call classmethod with myClass() instead of using myClass.myClassMethod()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i call classmethod with myClass() instead of using myClass.myClassMethod()
#1
i want to do
class foo(): # this class doesn't return and create object (object) , only runs main function when is called
    @classmethod
    def main(cls):
          print "main function is called with using foo()"

foo()

# output #
# main function is called with using foo()
is there a way to do this ?
Moderator snippsat: Fixed code tag look at BBcode help
Reply
#2
>>> class foo:
...     def say(self):
...         print("Hello!")

>>> bar = foo()

>>> bar.say()
Hello!

>>> say = bar.say

>>> say()
Hello!
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
You could rewrite __new__() to "forbid" creation of an object and to do some action, but why would someone sane want to do it ...
In [1]: class foo:
   ...:     def __new__(cls):
   ...:         print("foo")
   ...:         

In [2]: foo()
foo
Reply
#4
(Mar-23-2017, 05:08 PM)zivoni Wrote: You could rewrite __new__() to "forbid" creation of an object and to do some action, but why would someone sane want to do it ...
In [1]: class foo:
   ...:     def __new__(cls):
   ...:         print("foo")
   ...:         

In [2]: foo()
foo
thanks very very much but it works only python3, i use python2 this doesn't work on python2. can you give python2 example of this code ?
Reply
#5
works with new style class (inherits from object)
>>> class foo(object): 
...    def __new__(cls):
...      print "foo"
... 
>>> foo()
foo
Reply
#6
@zivoni, thanks very very much. it works
Reply
#7
(Mar-23-2017, 06:05 PM)harun2525 Wrote: @zivoni, thanks very very much. it works

But still... why?  Is there any real use for that, aside from making other people reading your code hate you?
Reply
#8
(Mar-23-2017, 06:26 PM)nilamo Wrote:
(Mar-23-2017, 06:05 PM)harun2525 Wrote: @zivoni, thanks very very much. it works

But still... why?  Is there any real use for that, aside from making other people reading your code hate you?

class clsOrHide(object):
    def __new__(cls, widget, name):
        cls.main(widget, name)

    @staticmethod 
    def main(widget, name):
        if name == "__main__":
            gtk.main_quit()
        else:
            widget.hide()

            return True

    @classmethod
    def del_event(cls, *args):
        cls.main(args[0], args[-1] )
or

def clsOrHide(widget, name):
      if name == "__main__":
            gtk.main_quit()
      else:
          widget.hide()

          return True

clsOrHide.del_event = lambda *args: clsOrHide(args[0], args[-1] )
which one is the better readable ?,  which one has the better code view ?

Edit:
sometime, we can't know what will need
Reply
#9
Neither are readable. It looks a little bit like an event handler that gets called multiple times for different reasons, which is... insane
Reply
#10
Classes shouldn't execute anything other than initialization code.
If you create methods that run main, and you instantiate two of these
classes what happens?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  @staticmethod and @classmethod akbarza 2 509 Jan-01-2024, 03:43 PM
Last Post: deanhystad
  How I can recognize that member is classmethod of staticmethod? AlekseyPython 0 1,831 Feb-17-2019, 07:01 AM
Last Post: AlekseyPython
  AttributeError: type object 'MyClass' has no attribute 'channel' chris0147 2 8,741 Sep-29-2017, 06:16 PM
Last Post: chris0147

Forum Jump:

User Panel Messages

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