Back to SDS/2 Parametric Scripts



A class definition does not create an instance. Create an instance by calling a class object as a function

(include arguments if required by the object). This creates an instance by calling method '__new__()' (this

is transparent to the user), which, in turn, calls class method '__init__()' if defined. Instance attributes

are then available and methods can be called. Methods will receive the argument 'self' automatically, so it

must be present in the argument list. If you want the instance to be displayed a certain way by 'print',

create special method '__repr__'. There are several other special methods - some are defined automatically

and some you can define in your class.


import sys, os

class TestClass(object):

    "This class will return your python version and platform."

    def __init__(self, s): = list(s)

    def version(self):

      return sys.version

    def platform(self):

      return sys.platform

    def __repr__(self):

        return "You have created an instance of 'TestClass'"


a = TestClass('create a list')

print a


print a.version()

print a.platform()



>>> You have created an instance of 'TestClass'

['c', 'r', 'e', 'a', 't', 'e', ' ', 'a', ' ', 'l', 'i', 's', 't']

2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)]


>>> dir(a)

['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__',\

'__init__', '__module__','__new__', '__reduce__', '__reduce_ex__', '__repr__',\

'__setattr__', '__str__', '__weakref__', 'data', 'platform', 'version']

>>> print a.__doc__

This class will return your python version and platform.

>>> print a.__class__

<class '__main__.TestClass'>

>>> for key in a.__dict__.keys():

... print 'instance.%s = %s' % (key, a.__dict__[key])

... = ['c', 'r', 'e', 'a', 't', 'e', ' ', 'a', ' ', 'l', 'i', 's', 't']