Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
classes and objects Posted by yankorezza on 1 Sept 2004 at 11:43 AM
Please somebody help. I am picking up python reasonably quickly, but going over certain tutorials again and again I just can't grasp the concept of classes. Can somebody please explain how to use them and what they do as if addressing a monkey, because I'm just banging my head against a wall!

Also, and feel free to laugh, but I can't understand exactly what is meant by an 'object'.

Thanks in advance!
Report
Re: classes and objects Posted by infidel on 1 Sept 2004 at 4:02 PM
: Please somebody help. I am picking up python reasonably quickly, but going over certain tutorials again and again I just can't grasp the concept of classes. Can somebody please explain how to use them and what they do as if addressing a monkey, because I'm just banging my head against a wall!
:
: Also, and feel free to laugh, but I can't understand exactly what is meant by an 'object'.

Classes and Objects aren't just for Python, these are basic object-oriented programming (OOP) concepts.

An object is any 'thing'. A box in an object. A door is an object. In the case of Python and many other languages, strings, integers, files, etc are all objects.

A class is what you might call the "type" or "kind" of an object. Some people like to think of a class as the blueprints and objects as the things created from the blueprints. You could use the same set of blueprints to build a dozen houses. Each house is a distinct thing, but they are all of the same class.

In Python terms, the object "FOOBAR" is an instance of a string class. 1 is an instance of an integer class.

Classes serve to encapsulate data with the functionality that manipulates that data.

class Rectangle():
    
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width


This class statement creates the blueprints for creating objects of type "Rectangle".

r1 = Rectangle(5, 10)
r2 = Rectangle(2, 3)


The above code uses the Rectangle class to create two objects, r1 and r2. To get the area of r1, we would call r1.area(). r1 and r2 are said to be "instances of class Rectangle". They are distinct objects with their own identities even though they are the same kind of thing.

When one kind of thing is a specialized version of some other thing, you can inherit the traits of the more general thing and extend it with only the new features. For example, all squares are special kinds of rectangles. We could create the following class:

class Square(Rectangle):

    def __init__(self, length):
        self.length = length
        self.width = length


Now we can create instances of the Square class. Note that we pass only one dimension:

s1 = Square(4)
s2 = Square(5)


Both of these objects inherit the area() function from the Rectangle class because the math works the same way for both Rectangles and Squares.

Let me know if you're still confused.


infidel

$ select * from users where clue > 0
no rows returned


Report
Re: classes and objects Posted by yankorezza on 2 Sept 2004 at 3:56 AM
Ah-ha! Got it. Thank you very much for that Infidel. You explained it very well and I understand perfectly now. I will copy the post for future reference if that's ok with you.
Report
Re: classes and objects Posted by infidel on 2 Sept 2004 at 8:21 AM
: Ah-ha! Got it. Thank you very much for that Infidel. You explained it very well and I understand perfectly now. I will copy the post for future reference if that's ok with you.

Glad I could help.


infidel

$ select * from users where clue > 0
no rows returned





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.