|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Classes in Python
I recently started learning Python, but, I'm having some problems with classes.. Where can I find information about making something like this in Python:
Code:
class B : A {
}
B *obj = new B;
A *obj = (A*)B;
Any suggested websites that might shed more light on this stuff? (and classes and whatnot in general) TIA |
|
#2
|
|||
|
|||
|
|
|
#3
|
||||
|
||||
|
What on earth does that class do. Obviously C/C++ because of the pointers there.
Mark. |
|
#4
|
||||
|
||||
|
The equivalent python code would be:
Code:
class A:
pass
class B(A):
pass
b = B()
a = b
A more practical example might be something like this: Code:
class animal:
def say(self):
print "No sound assigned"
class cat(animal):
def say(self):
print "Meow!"
class dog(animal):
def say(self):
print "Woof!"
def speak(animal):
animal.say()
garfield = cat()
tom = cat()
snoopy = dog()
spike = dog()
pets = [garfield, tom, snoopy, spike]
for pet in pets:
speak(pet)
# call the base class say() method for spike.
animal.say(spike)
Hope this helps ![]() Note that if you don't have to invoke the base class method (i.e. animal.say()) explicitly, there's actually no need for the base class at all. This is because python is not strongly typed and methods/attributes are checked at runtime. You can get away with the following code in python, since both the cat and dog classes have a say() method: Code:
class cat:
def say(self):
print "Meow!"
class dog:
def say(self):
print "Woof!"
def speak(creature):
creature.say()
garfield = cat()
tom = cat()
snoopy = dog()
spike = dog()
pets = [garfield, tom, snoopy, spike]
for pet in pets:
speak(pet)
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month Last edited by Scorpions4ever : March 3rd, 2004 at 03:56 PM. |
|
#5
|
|||
|
|||
|
Not that it really matters: Python is strongly and dynamically typed. Scorpions4ever probably meant that Python is not statically typed. Anyway; the example clears up everything.
|
|
#6
|
|||
|
|||
|
Ah! Wonderful! Thanks!
I just couldn't find any good examples, but that is perfect. ![]() Thanks again. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Classes in Python |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|