Hi there guys, i'm just learning python and have been finding it interesting, but i've been finding it hard to understand classes and objects. Please is there a very explanatory tutorial or can you be of help, looking forward to your replies.

Thanks so much woooee, i actually have thinkpython and dive into python, but that third one i don't have and will like to try that. I don't just get it. And it's really disturbing me because everything in python is centered around this, since it's OOP . Is there anyway you can simplify it for me.

Classes describe objects
Objects hold data and do work

If you can imagine your program as "He does something, and in order to do it, he asks her to do something, and she needs help from several of her friends..." then each of the 'he', 'she' and 'friend' might be objects; and the object's class specifies what the object knows how to do, what data it has, etc.

Python is not necessarily object oriented: I've written many useful utilities that had not a class in them (except in the library modules). On the other hand, it is very easy to use Python in an object oriented way; and you certainly should 'get it' because when OO is good, it is very very good...

I once posted simple stack class as toy project to improve to code snippets. Recently I had one interesting experience replacing a stack class in real code as I had not that part of the code. i posted it as last reply of that thread. I hope it could be usefull. Before diving into class methods, decorators, inspection etc

OO is little strange beast, as it often is in conflict with functional programming. Often it is not so far from normal modular program design. Main new thing I think is inheritance.

An object is just an abstraction of data and functions.

An object has attributes (data) and methods (functions that interact with its attributes).

A class is a blueprint describing the objects created from it.

When you write a class you are 'declaring' a class.

An object created from a class is an 'instance' of that class.

Objects are most useful when the systems you are modeling in your program can be abstracted as objects.

Anything you can refer to with a noun is a potential candidate.

I recommend that people learning object-oriented programming model concrete objects.

http://www.chompchomp.com/terms/concretenoun.htm

A simple explanation of a Python class object ...

# a look at Python's class constructor, method and instance
# class names are capitalized by convention to aid readability

class Animal:
    def __init__(self, animal, sound):
        """
        The constructor __init__() brings in external
        parameters when an instance of the class is created,
        self keeps track of the specific instance and makes
        instance variables global to the class so the class
        methods can use them
        """
        self.animal = animal
        self.sound = sound
        
    def animal_sound(self):
        """
        a class method has self for the first argument
        note that self.animal and self.sound are global 
        to the class and specific to the instance
        """
        print( "The %s goes %s" % (self.animal, self.sound) )


# create an instance of the class
# you have to supply the animal and it's sound
# as shown in the class __init__() method
cow = Animal('cow', 'mooh')

# create another instance
dog = Animal('dog', 'woof')

# now let's do something with each instance
# the instance name is dot_connected to the method
cow.animal_sound()  # The cow goes mooh

dog.animal_sound()  # The dog goes woof
commented: Thanks for the explanation. +1
commented: Very understandable explanation. +0

I think i'm gettin something, let me try this.

A simple explanation of a Python class object ...

# a look at Python's class constructor, method and instance
# class names are capitalized by convention to aid readability

class Animal:
    def __init__(self, animal, sound):
        """
        The constructor __init__() brings in external
        parameters when an instance of the class is created,
        self keeps track of the specific instance and makes
        instance variables global to the class so the class
        methods can use them
        """
        self.animal = animal
        self.sound = sound
        
    def animal_sound(self):
        """
        a class method has self for the first argument
        note that self.animal and self.sound are global 
        to the class and specific to the instance
        """
        print( "The %s goes %s" % (self.animal, self.sound) )


# create an instance of the class
# you have to supply the animal and it's sound
# as shown in the class __init__() method
cow = Animal('cow', 'mooh')

# create another instance
dog = Animal('dog', 'woof')

# now let's do something with each instance
# the instance name is dot_connected to the method
cow.animal_sound()  # The cow goes mooh

dog.animal_sound()  # The dog goes woof

Thanks I've just finished reading about classes and object, i still intend to read more but i see that you really did a good job explaining the various concepts, You should post this in the tutorial section so that other people can gain from it.
Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.