Python Forum
Listing All Methods Of Associated With A Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Listing All Methods Of Associated With A Class
#1
Hello

I wanted to ask whether it was possible to list all the methods associated with a Class for example:-

I have created the following class:-

class Human():

    
    def __init__(self, sex, eyecolor, height, yearborn):
     
        self.sex = sex
        self.eyecolor = eyecolor
        self.height = height
        self.yearborn = yearborn
        
        print("Object", self.sex, self.eyecolor, self.height, self.yearborn, "Has been created")
        
    
    def age(self):
        currentdate = date.today()
        a = currentdate.year - self.yearborn
        return a
    
    def display(self):
        print(self.sex)
        print(self.eyecolor)
        print(self.height)
        print(self.yearborn)
        
In the above class I have created I have 2 x methods age() and display().

My question is this: is there any Python code that allows me to enter the name of the class and the output are all the methods associated with that class?

Thank you.
Reply
#2
import inspect


class Human:

    def __init__(self, sex, eyecolor, height, yearborn):

        self.sex = sex
        self.eyecolor = eyecolor
        self.height = height
        self.yearborn = yearborn

        print("Object", self.sex, self.eyecolor,
              self.height, self.yearborn, "Has been created")

    def age(self):
        currentdate = date.today()
        a = currentdate.year - self.yearborn
        return a

    def display(self):
        print(self.sex)
        print(self.eyecolor)
        print(self.height)
        print(self.yearborn)

print(inspect.getmembers(Human, inspect.isfunction))
print(inspect.getmembers(Human(1, 2, 3, 4), inspect.ismethod))
Output:
[('__init__', <function Human.__init__ at 0x000001DBC4727160>), ('age', <function Human.age at 0x000001DBC4A58CA0>), ('display', <function Human.display at 0x000001DBC4A58D30>)] Object 1 2 3 4 Has been created [('__init__', <bound method Human.__init__ of <__main__.Human object at 0x000001DBC4708FD0>>), ('age', <bound method Human.age of <__main__.Human object at 0x000001DBC4708FD0>>), ('display', <bound method Human.display of <__main__.Human object at 0x000001DBC4708FD0>>)]
Reply
#3
Alterative dir(class).
>>> dir(Human)
[.....
 '__subclasshook__',
 '__weakref__',
 'age',
 'display']
To just show method and not all special methods.
>>> [m for m in dir(Human) if not m.startswith('__')]
['age', 'display']
Reply
#4
help(class)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  access share attributed among several class methods drSlump 0 140 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 535 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  too many methods in class - redesign idea? Phaze90 3 978 Mar-05-2021, 09:01 PM
Last Post: deanhystad
  Special Methods in Class Nikhil 3 947 Mar-04-2021, 06:25 PM
Last Post: Nikhil
  Listing files with glob. MathCommander 9 1,798 Oct-26-2020, 02:04 AM
Last Post: MathCommander
  Listing data from a list ebolisa 1 752 Sep-29-2020, 02:24 PM
Last Post: DeaD_EyE
  cant able to make methods interact with each other in the class jagasrik 2 745 Sep-16-2020, 06:52 PM
Last Post: deanhystad
  Listing Attributes of Objects & Classes JoeDainton123 4 1,012 Aug-28-2020, 05:27 AM
Last Post: ndc85430
  Question about naming variables in class methods sShadowSerpent 1 880 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Listing groups tharpa 2 1,259 Nov-26-2019, 07:25 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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