How do you write this in python

Ian J Cottee ian at cottee.org
Wed Oct 6 01:24:42 EDT 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 
'objects'

>     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.

>         self.name = name
>         self.email = 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__ 
method.

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>', self.name, '<br>\n',
>                    '<b>Email: </b>', self.email, '<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 (self.name and self.email).

>         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 alik.com')
> zainab = Card('Zainab', 'zainab at zainab.com')

Now we create two cards. The first card (an instance of the class Card) 
is called ali. ali.name will be 'Ali' and ali.email will be 
'alik at alik.com'. 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 alik.com'. In this case you'd get

    <hr>
    <b>Name: </b>Ali<br>
    <b>Email: </b>alik at alik.com<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

    <hr>
    <b>Name: </b>Zainab<br>
    <b>Email: </b>zainab at zainab.com<br>


and that is how we do that

Ian



More information about the Python-list mailing list