Listing All Methods Of Associated With A Class - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Listing All Methods Of Associated With A Class (/thread-33593.html) |
Listing All Methods Of Associated With A Class - JoeDainton123 - May-09-2021 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. RE: Listing All Methods Of Associated With A Class - Yoriz - May-09-2021 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))
RE: Listing All Methods Of Associated With A Class - snippsat - May-09-2021 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'] RE: Listing All Methods Of Associated With A Class - deanhystad - May-10-2021 help(class) |