How do you write this in python

Ian J Cottee ian at
Wed Oct 6 07:24:42 CEST 2004

Ali wrote:
> ok that looks close but can someone explain it to me section by section please?

No idea what your experience/knowledge is so here's a quick noddy run 
through. Heed Steven's advice however and go through a tutorial.

> class Card:

Defines a class called 'Card'. You can think of a class as being a 
template for different types of card you want to produce. Each card you 
make will be an instance of the Card class. We also call those instances 

>     def __init__(self, name, email):

Tells python that when we create a class we will pass it two parameters 
- name and email. This is a a 'method' of the class. A method is like a 
function or subroutine (maybe an action if you like) and defines some 
behaviour specific (in this case) to cards. All class methods take at 
least one parameter which is 'self'. self refers to the object itself.

> = name
> = email

As well as methods we also have properties. If you know other languages 
then you can think of them as variables which exist only for instances 
of this class. In this case we say that the class object we have just 
created will have two properties - 'name' and 'email'. We assign these 
properties the values of the two arguments we defined in our __init__ 

You can assign properties whenever you want - it does not have to be 
done in the __init__ method.

>     def tohtml(self):
>         output = [ '<hr>\n',
>                    '<b>Name: </b>',, '<br>\n',
>                    '<b>Email: </b>',, '<br>\n' ]

This defines another method called 'tohtml'. In this case we create a 
list of strings. Lists are useful ways of manipulating data in Python - 
you can think of them a bit like arrays if you know other programming 
languages. Note that in our list (which is called output) we have both 
raw text strings (string literals) and also our the values of our 
instance variables ( and

>         return ''.join(output)

our tohtml method returns a value to whatever calls it. In this case it 
returns the empty string '' concatenated with all the strings in our 
output variable.

> ali = Card('Ali', 'alik at')
> zainab = Card('Zainab', 'zainab at')

Now we create two cards. The first card (an instance of the class Card) 
is called ali. will be 'Ali' and will be 
'alik at'. Remember, these are set in our __init__ code. The 
second is called zainab and it has it's own instance variables.

> print ali.tohtml()

This calls the tohtml method for ali (the '()' at the end shows us we 
are calling a method rather than just returning an instance variable). 
In this case tohtml will concatenate the string literals with the 
instance variables set to 'Ali' and 'alik at'. In this case you'd get

    <b>Name: </b>Ali<br>
    <b>Email: </b>alik at<br>

(note that within the string literals, \n is replaced by a new line)

> print zainab.tohtml()

do the same for the zainab object. It would give us

    <b>Name: </b>Zainab<br>
    <b>Email: </b>zainab at<br>

and that is how we do that


More information about the Python-list mailing list