How do you write this in python

A

Ali

I have the following webpage with a javasctript in it:

<html>
<head>
<title>Custom Objects Test</title>
<script language="javascript">
function PrintCard() {
line1 = "<hr>\n";
line2 = "<b>Name: </b>" + this.name + "<br>\n";
line3 = "<b>Email: </b>" + this.email + "<br>\n";
document.write(line1, line2, line3);
}
function Card(name,email) {
this.name = name;
this.email = email;
this.PrintCard = PrintCard;
}
</script>
</head>
<body>
<script language="javascript">
ali = new Card("Ali", "(e-mail address removed)");
zainab = new Card("Zainab", "(e-mail address removed)");

ali.PrintCard();
zainab.PrintCard();
</script>
</body>
</html>

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.

If you have questions plz dont hesitate to ask. Please Help. Thank you
:)
 
A

Alex Martelli

Ali said:
function PrintCard() { ...
}
function Card(name,email) {
this.name = name;
this.email = email;
this.PrintCard = PrintCard;
} ...
ali = new Card("Ali", "(e-mail address removed)");
zainab = new Card("Zainab", "(e-mail address removed)"); ...
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, '(e-mail address removed)')
zainab = Card('Zainab', '(e-mail address removed)')


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
 
S

Steven Rumbalski

Ali said:
I have the following webpage with a javasctript in it:

<html>
<head>
<title>Custom Objects Test</title>
<script language="javascript">
function PrintCard() {
line1 = "<hr>\n";
line2 = "<b>Name: </b>" + this.name + "<br>\n";
line3 = "<b>Email: </b>" + this.email + "<br>\n";
document.write(line1, line2, line3);
}
function Card(name,email) {
this.name = name;
this.email = email;
this.PrintCard = PrintCard;
}
</script>
</head>
<body>
<script language="javascript">
ali = new Card("Ali", "(e-mail address removed)");
zainab = new Card("Zainab", "(e-mail address removed)");

ali.PrintCard();
zainab.PrintCard();
</script>
</body>
</html>
Are you looking for something like this?

class Card:
def __init__(self, name, email):
self.name = name
self.email = email
def tohtml(self):
output = [ '<hr>\n',
'<b>Name: </b>', self.name, '<br>\n',
'<b>Email: </b>', self.email, '<br>\n' ]
return ''.join(output)

ali = Card('Ali', '(e-mail address removed)')
zainab = Card('Zainab', '(e-mail address removed)')

print ali.tohtml()
print zainab.tohtml()
 
S

Steven Rumbalski

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

Someone could, bu I have a better suggestion. Go to python.org and pick a
tutorial that best fits your needs as a learner. After you've put in the
groundwork, please come back and ask specific questions that show you are
grappling with learning python. You will find the fine people in this
newsgroup very helpful when you learn to ask pointed questions that you
have already tried to answer for yourself.

--Steven Rumbalski
 
I

Ian J Cottee

Ali said:
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', '(e-mail address removed)')
zainab = Card('Zainab', '(e-mail address removed)')

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
'(e-mail address removed)'. 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 '(e-mail address removed)'. In this case you'd get

<hr>
<b>Name: </b>Ali<br>
<b>Email: </b>[email protected]<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>[email protected]<br>


and that is how we do that

Ian
 
A

Ali

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

This cought my attention so I entered this into the shell to try to do
this very thing:
def setProperties(self, name, email):
self.name = name
self.email = email
def show(self):
print self.name + '/' + self.email

I got the following error:

Traceback (most recent call last):
File "<pyshell#27>", line 1, in ?
ali = card('ali','(e-mail address removed)')
TypeError: this constructor takes no arguments

I thought you said I could assign the properties at any place so I
defined them in the setProperties() method. What did I do wrong?
 
J

Jeff Shannon

Ali said:
ok that makes some sense.




This cought my attention so I entered this into the shell to try to do
this very thing:



def setProperties(self, name, email):
self.name = name
self.email = email
def show(self):
print self.name + '/' + self.email




I got the following error:

Traceback (most recent call last):
File "<pyshell#27>", line 1, in ?
ali = card('ali','(e-mail address removed)')
TypeError: this constructor takes no arguments

I thought you said I could assign the properties at any place so I
defined them in the setProperties() method. What did I do wrong?

You can assign attributes whenever you want (and what you're doing is
setting attributes; properties are something special, and different).

However, when you create a new object by calling the class constructor,
__init__() is the only function that will be automatically called. If
you don't define __init__(), then (in essence) Python will use a dummy
__init__() that takes only self as an argument, and does nothing.
However, you've tried to pass several arguments to __init__(), but you
didn't define your own...

The class you've defined can't be created with name or email attributes,
since you haven't provided an __init__() to catch them. However, you
can create a 'blank' card and then add the name and email later.
Traceback (most recent call last):

Jeff Shannon
Technician/Programmer
Credit International
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top