Data types

Tim Jarman tmj at SPAMLESSjarmania.com
Thu Mar 24 17:01:04 EST 2005


vahmad70 at yahoo.com wrote:

> I am new to python and learning it. Can you please give me a simple
> example of user defined type through class mechanism.

Python 2.4 (#1, Dec 31 2004, 17:21:43)
[GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Gumby(object):
...     def __init__(self, body_part):
...             self.body_part = body_part
...     def speak(self):
...             print "My %s hurts!" % self.body_part
...
>>> g = Gumby("brain")
>>> g
<__main__.Gumby object at 0x402c570c>
>>> g.speak()
My brain hurts!


Despite what Mr Gumby just said, defining your own classes is pretty
painless in Python. Check out the Tutorial, especially section 9:
http://www.python.org/doc/2.4/tut/node11.html

Enjoy!

-- 
Website: www DOT jarmania FULLSTOP com



More information about the Python-list mailing list