How do you write this in python

Alex Martelli aleaxit at yahoo.com
Sun Oct 3 04:37:12 EDT 2004


Ali <alikakakhel3 at hotmail.com> wrote:
   ...
> function PrintCard() {
   ...
> }
> function Card(name,email) {
>       this.name = name;
>       this.email = email;
>       this.PrintCard = PrintCard;
> }
   ...
> ali = new Card("Ali", "alik at alik.com");
> zainab = new Card("Zainab", "zainab at zainab.com");
   ...
> The script in this page, has a function (Card) that is used to create
> an object with its own properties and methods (ali and zainab in this
> script). I was wondering if this was possible in python.

Sure, though we'd normally use a class statement instead:

class Card:
    def __init__(self, name, email):
        self.name = name
        self.email = email
    def PrintCard(self):
        ''' whatever... '''

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


If for some weird reason you're keen to make Card a factory function
rather than a class, that can be arranged, too.  But the normal way in
Python is just to use and define classes.


Alex



More information about the Python-list mailing list