[Tutor] designing a class

Runsun Pan python.pan at gmail.com
Fri Jan 27 08:10:54 CET 2006


#------------------------------------ initialization of class

If you want to be able to give initial values to the class when it
is instantialized, you do it in the __init__ function that Terry
mentioned:

class People(object):
        def __init__(self, firstname='Chris', lastname='Spears'):
             self.firstname = firstname
             self.lastname = lastname

That is, instead of putting

        lastname = 'Spears'
        firstname = Chris

right under "class People(object)", you put them inside the __init__.

This will allow some user values be entered during the instantiation:

p = People( firstname = 'Christopher')



On 1/27/06, Runsun Pan <python.pan at gmail.com> wrote:
> On 1/26/06, Christopher Spears <cspears2002 at yahoo.com> wrote:
>
> > headlights.  Where should I begin?  How do I go about
> > designing a new class?
>
> Some starter 101:
>
> First, recognize that a class is a compound type that is not only a type
> but binds other variables and methods with it.
>
> #----------------------------------- class definition
> class People(object):
>         lastname = 'Spears'
>         firstname = Chris
>
> object is the parent of People from where People inherits all of its
> definitions and behavior (functions). It's also called "subclassing" --
> subclassing object to get People.
>
> #----------------------------------- instantiation
> p = People( )
>
> p is called "an instance of class People"
>
> >>> p.lastname
> Spears
> >>> p.firstname
> Chris
> >>> p.firstname = 'runsun'
> >>> p.firstname
> runsun
>
>
> ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
> Runsun Pan, PhD
> python.pan at gmail.com
> Nat'l Center for Macromolecular Imaging
> http://ncmi.bcm.tmc.edu/ncmi/
> ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
>


--
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
Runsun Pan, PhD
python.pan at gmail.com
Nat'l Center for Macromolecular Imaging
http://ncmi.bcm.tmc.edu/ncmi/
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~


More information about the Tutor mailing list