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", "al**@alik.com");
zainab = new Card("Zainab", "za****@zainab.com");
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
:) 3 1659
Ali <al**********@hotmail.com> wrote:
... function PrintCard() {
... } function Card(name,email) { this.name = name; this.email = email; this.PrintCard = PrintCard; }
... ali = new Card("Ali", "al**@alik.com"); zainab = new Card("Zainab", "za****@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, 'al**@alik.com')
zainab = Card('Zainab', 'za****@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
Ali wrote: 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", "al**@alik.com"); zainab = new Card("Zainab", "za****@zainab.com");
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', 'al**@alik.com')
zainab = Card('Zainab', 'za****@zainab.com')
print ali.tohtml()
print zainab.tohtml()
O I see... Thank you so much! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: MiLF |
last post by:
Is it possible to write a Audio CD Player by using python only?
|
by: Nick Evans |
last post by:
Hello there,
I have been on and off learning to code (with python being the second
language I have worked on after a bit of BASIC). What I really want to know
is, if you are going to actually...
|
by: Xah Lee |
last post by:
i've started to read python tutorial recently.
http://python.org/doc/2.3.4/tut/tut.html
Here are some quick critique:
quick example:
If the input string is too long, they don't truncate it,...
|
by: welch |
last post by:
while taking some rough disk performance measures on windows machines,
and snooping with FileMon, i've noticed some odd behavior
here's the little nul-writer i'm running:
def writeTest(nBlocks,...
|
by: Levi Campbell |
last post by:
Hi, I'm thinking about writing a system for DJing in python, but I'm
not sure if Python is fast enough to handle the realtime audio needed
for DJing, could a guru shed some light on this subject...
|
by: Raj |
last post by:
Hi,
We just executed a project with Python using TG. The feedback was to
use more python like programming rather than C style code executed in
Python. The feedback is from a Python purist and...
|
by: Matthew Warren |
last post by:
I have the following piece of code, taken from a bigger module, that
even as I was writing I _knew_ there were better ways of doing it, using
a parser or somesuch at least, but learning how wasn't...
|
by: Thomi Aurel RUAG A |
last post by:
Hy
I'm using Python 2.4.2 on an ARM (PXA-270) platform (linux-2.6.17).
My Goal is to write a list of bytes down to a file (opened in binary
mode) in one cycle. The crux is that a '0x0a' (line...
|
by: dmoore |
last post by:
Hi folks,
I've seen the following issue come up in multiple posts to this
mailing list:
I have a python program that spawns a child process with popen or
popen2 or popen3 or popen2.popen2...
|
by: madsornomads |
last post by:
Hi all,
I have a problem with reading from a Java server after I have written
to it - it just hangs. It works fine if I just write to the server and
not try to write. I have read the HOWTO on...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |