#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    2
    Rep Power
    0

    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. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    What on earth does that class do. Obviously C/C++ because of the pointers there.

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    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)
    Last edited by Scorpions4ever; March 3rd, 2004 at 04:56 PM.
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    2
    Rep Power
    0
    Ah! Wonderful! Thanks!

    I just couldn't find any good examples, but that is perfect.

    Thanks again.

IMN logo majestic logo threadwatch logo seochat tools logo