Prabu writes:[color=blue]
> I'm new to python, so excuse me if i'm asking something dumb.[/color]
Hey, everyone had to start someplace, we try hard to be nice
to newbies here. You can also check out the beginners list
(http://mail.python.org/mailman/listinfo/tutor), where they
SPECIALIZE in being clear to newbies.
[color=blue]
> Does python provide a mechanism to implement virtual functions?[/color]
Yes. If by "virtual function" you mean what is meant in C++, then
*all* functions in Python are virtual.
[color=blue]
> Can you please give a code snippet also...:)[/color]
Of course!
G:\>python
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
>>> class Animal(object):[/color][/color][/color]
... def speak(self):
... pass
...[color=blue][color=green][color=darkred]
>>> class Dog(Animal):[/color][/color][/color]
... def speak(self):
... print 'Woof'
...[color=blue][color=green][color=darkred]
>>> class Cat(Animal):[/color][/color][/color]
... def speak(self):
... print 'Miow'
...[color=blue][color=green][color=darkred]
>>> class Giraffe(Animal) :[/color][/color][/color]
... pass
...[color=blue][color=green][color=darkred]
>>> dog = Dog()
>>> cat = Cat()
>>> giraffe = Giraffe()
>>> dog.speak()[/color][/color][/color]
Woof[color=blue][color=green][color=darkred]
>>> cat.speak()[/color][/color][/color]
Miow[color=blue][color=green][color=darkred]
>>> giraffe.speak()[/color][/color][/color]
Notice how I overrode the "virtual" method speak() in Dog and Cat
and just for a change I left the parent method in place for Giraffe.
Does that answer your question?
-- Michael Chermside
> I'm new to python, so excuse me if i'm asking something dumb.[/color]
Hey, everyone had to start someplace, we try hard to be nice
to newbies here. You can also check out the beginners list
(http://mail.python.org/mailman/listinfo/tutor), where they
SPECIALIZE in being clear to newbies.
[color=blue]
> Does python provide a mechanism to implement virtual functions?[/color]
Yes. If by "virtual function" you mean what is meant in C++, then
*all* functions in Python are virtual.
[color=blue]
> Can you please give a code snippet also...:)[/color]
Of course!
G:\>python
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
>>> class Animal(object):[/color][/color][/color]
... def speak(self):
... pass
...[color=blue][color=green][color=darkred]
>>> class Dog(Animal):[/color][/color][/color]
... def speak(self):
... print 'Woof'
...[color=blue][color=green][color=darkred]
>>> class Cat(Animal):[/color][/color][/color]
... def speak(self):
... print 'Miow'
...[color=blue][color=green][color=darkred]
>>> class Giraffe(Animal) :[/color][/color][/color]
... pass
...[color=blue][color=green][color=darkred]
>>> dog = Dog()
>>> cat = Cat()
>>> giraffe = Giraffe()
>>> dog.speak()[/color][/color][/color]
Woof[color=blue][color=green][color=darkred]
>>> cat.speak()[/color][/color][/color]
Miow[color=blue][color=green][color=darkred]
>>> giraffe.speak()[/color][/color][/color]
Notice how I overrode the "virtual" method speak() in Dog and Cat
and just for a change I left the parent method in place for Giraffe.
Does that answer your question?
-- Michael Chermside