How to List Functions in a Class in Python

Python grants programmers the freedom to write code in whatever paradigm provides the best match of the project's requirements and the programmer's preference and skill. Python code may be procedural, object-oriented, functional, imperative and reflective --- or a mixture of them all. Python's object-oriented programming design includes built-in tools to help the programmer access all objects encapsulated within the class. Python's "dir()" function returns a list of the attributes of any Python class, including the class's methods --- the functions within the class --- and its variables.

Things You'll Need

  • Plain-text editor
Show More

Instructions

    • 1

      Launch the plain-text editor application on your sytem, such as NotePad on the PC or Jedit, Komodo Edit, Smultron, BBEdit or TextMate on Mac OS X.

    • 2

      Enter the following code into the text editor exactly as shown.

      class Square:

      def __init__(self, side):

      self.side = side

      def calculateSquareArea(self):

      return self.side**2

      class Circle:

      def __init__(self, radius):

      self.radius = radius

      def calculateCircleArea(self):

      import math

      return math.pi*(self.radius**2)

    • 3

      Click on the "File" menu and select "Save." Save the text file with the name "myclass.py".

    • 4

      Click the "File" menu and then select the appropriate option to close the text editor.

    • 5

      Launch the system terminal, console or command line. At the command line prompt, type "python" and then press the "Enter" key. This should load the Python interpreter.

    • 6

      Type the following at the Python command prompt:

      execfile("myclass.py")

      Then press the "Enter" key.

    • 7

      Type the following at the Python command prompt:

      dir(Square)

      Then press the "Enter" key. The function "calculateSquareArea" should be displayed.

    • 8

      Type the following at the Python command prompt:

      dir(Circle)

      Then press the "Enter" key. The function "calculateCircleArea" should be displayed.

Tips & Warnings

  • Some installations require the path to the directory containing the Python binaries. For example, if Python 2.7 is the installed version, to load the Python interpreter, enter "c:\python27\python".

  • Indentation matters in Python programming. Indent code with the "Tab" key or with blank spaces, but do no use both within the same script. Mixing the two may result in the interpreter loading the code incorrectly or not at all.

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Learn Python Programming Online

    Python is a powerful programming language that can be used in a variety of applications. It's also one of the easier languages...

  • How to Install Python for Windows

    Change to your Python directory. If you accepted the default Python directory, type "cd C:\Python25" into the command prompt and hit enter....

  • How to Append to a List in Python

    The Python programming language stores data in a variety of collections, including a list. The list collection stores a number of items...

  • How to Edit Cron in CentOS

    Cron is an application that executes tasks at predefined times. It is one of the utilities integrated into UNIX-based operating systems. CentOS,...

Related Ads

Featured