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
  Class test : good way to split methods into several files paul18fr 4 402 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Structuring a large class: privite vs public methods 6hearts 3 1,014 May-05-2023, 10:06 AM
Last Post: Gribouillis
  Listing directories (as a text file) kiwi99 1 802 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Read directory listing of files and parse out the highest number? cubangt 5 2,249 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  access share attributed among several class methods drSlump 0 1,038 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 2,497 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  too many methods in class - redesign idea? Phaze90 3 2,452 Mar-05-2021, 09:01 PM
Last Post: deanhystad
  Special Methods in Class Nikhil 3 2,221 Mar-04-2021, 06:25 PM
Last Post: Nikhil
  Listing files with glob. MathCommander 9 4,812 Oct-26-2020, 02:04 AM
Last Post: MathCommander
  Listing data from a list ebolisa 1 1,707 Sep-29-2020, 02:24 PM
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