A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

A Basic Python Class

This is the basic structure of a Python class.

>>> class Simplex:
...     "An example class"""
...     def __init__(self, x, y):
...             self.a = x
...             self.b = y
...     def addition(self):
...             return self.a + self.b
>>> z = Simplex(3,4)
>>> z.a, z.b
(3, 4)
>>> z.addition()
>>> z.__doc__
'An example class'
  1. I created the class
  2. Instantiated the class and gave 3 and 4 as parameters.
  3. Call a method
  4. Test out __doc__

Source: http://docs.python.org/tutorial/classes.html

This entry was posted in General and tagged , , , . Bookmark the permalink.

One Response to A Basic Python Class

  1. I liked this session. I am new to Python, but would love to learn more.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>