7 Replies - 1274 Views - Last Post: 25 July 2011 - 04:30 PM Rate Topic: -----

#1 2DZombie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 13-July 11

Understanding 'Classes' and 'Objects'

Posted 24 July 2011 - 01:21 AM

This is technically not limited to Python, but it is something I need to understand in order for me to move forward with the language. Python is my first language and I'm struggling with understanding classes and objects.

You'd think understanding Objects would be rudimentary when it comes to an OOP language, but the definitions I'm finding are vague and rely solely on abstract terms (or maybe I'm just dense.)

What exactly is a class? It creates an 'instance' of itself and serves as a specific blueprint for something, but how is this different from a function? What is a solid definition for a class?

What denotes an object? Do classes create objects? Are classes objects in themselves?


Sorry if these are stupid questions, but I've been mulling over this all night and finally broke down and decided to ask those who are smarter than me. :sweatdrop:

Is This A Good Question/Topic? 2
  • +

Replies To: Understanding 'Classes' and 'Objects'

#2 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Understanding 'Classes' and 'Objects'

Posted 24 July 2011 - 02:36 AM

Don't feel put off. Lots of people have difficulty grasping the concepts of OO programming!

Think of a class a blueprint (as you said), and an object as the realisation of that blueprint. For example, the blueprint of a Car could be considered a class, where as the actual car that results from the blue print clearly exists, and is an object.

What makes OO hard to grasp in the beginning, when using Python, is that everything is an object in Python. Classes, functions, gravity, everything. This isn't so in lots of other languages.

So, for simplicity's sake let's pretend we're talking about a language where things are only objects when they must be; meaning functions, classes and everything else are just what they seem.

With that in mind, what is a function? A function is something that takes input (maybe) and returns something (maybe). Nothing more, nothing less.

By comparison, an object has attributes. E.g. a Person object has a name, date of birth, address, etc. What does a function have? Nothing.

Does that make sense? If anything is unclear, tell me and I'll clarify.
Was This Post Helpful? 3
  • +
  • -

#3 2DZombie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 13-July 11

Re: Understanding 'Classes' and 'Objects'

Posted 24 July 2011 - 10:47 PM

It makes more sense and after reading some more and is slowly getting easier to understand. (Although still a tad brain scrambling) What are some of the benefits of using a class over a simple function?

For example:

class Hypotenuse:


    def __init__(self, a = 0, b = 0):
        self.a = a
        self.b = b

    def hypot(self):
        return  ((self.a ** 2) + (self.b ** 2)) ** 0.5




A class that contains a method that finds the hypotenuse of a triangle using the first two sides as optional arguments. But wouldn't it be easier to just cut out the middleman and just go with the function in of itself?
def hypot(a, B)/>:
        return  ((a ** 2) + (b ** 2)) ** 0.5



I understand that classes chunk similar functions (or I guess methods) together, but what is the overall purpose? Is it that they can instance values within that specific class in order to be acted upon by multiple methods?


(Oh and another question. Does the __init__ method just work to set the instance's initial attributes and values, or does it have other purposes?)

This post has been edited by 2DZombie: 24 July 2011 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Understanding 'Classes' and 'Objects'

Posted 25 July 2011 - 04:13 AM

Basically classes provide a level of abstraction to represent real world objects with ease, that just isn't possible with functions. With something like Hypotenuse, functions make more sense because hypotenuse is calulated using inputs, i.e. you can use a function for it.

However, for more complex objects like a Person (with a name, date of birth, and an address), a Bank Account (with a balance, account number and account name) and a Staff Member (with payroll info, name, etc) it makes much more sense to use classes.

A few examples:

class Point(object):
   def __init__(self, x, y):
      """Takes two integers, x and y, for the coordinates"""
      self.x = x
      self.y = y

class Triangle(object):
   def __init__(self, a, b, c):
      """Takes three Points to comprise a triangle"""
      self.a = a
      self.b = b
      self.c = c

   def centroid(self):
      # code for finding the centre of a Triangle
      pass

class Person(object):
   def __init__(self, name, address, date_of_birth):
      self.name = name
      self.address = address
      self.date_of_birth = date_of_birth

   def go_to_work(self):
      print "ho hum, off to work."



So hopefully you can see what I'm getting at. Basically a class is great for providing an abstraction of a real world object.

And yes the __init__ method works as you stated. Basically when you instantiate an object, you may want to set attributes, call functions, lots of things.

Say for example you wanted to modify your Person class above to write to file every time you created a new one (who knows why, but anyway)...

class Person(object):
   def __init__(self, name, address, date_of_birth):
      self.name = name
      self.address = address
      self.date_of_birth = date_of_birth

      with open(name, 'w') as f:
         f.write(name + '\n')
         f.write(address + '\n')
         f.write(date_of_birth + '\n')

   def go_to_work(self):
      print "ho hum, off to work."





Which would mean that every time you call Person('name', 'address', 'dob') that object would be written to file.
Was This Post Helpful? 2
  • +
  • -

#5 2DZombie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 13-July 11

Re: Understanding 'Classes' and 'Objects'

Posted 25 July 2011 - 02:28 PM

That makes a lot more sense. So a class is to create objects that have their own attributes and functionality versus just passing an input through a function. Like a car contains the 'drive' method in itself as opposed to having the car rely on a external 'drive' function.
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Understanding 'Classes' and 'Objects'

Posted 25 July 2011 - 02:47 PM

Forgive me if you already understand this, I think your wording is a little fuzzy.

You model objects as real world objects, so a car would never rely on an "external 'drive' function" - the fact is that you can drive a car in the real world, so it contains that method theoretically. The car object encapsulates the methods it uses inside the object. You cannot get in a car and call a function to make the car drive now, can you?!

This post has been edited by Simown: 25 July 2011 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

#7 2DZombie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 13-July 11

Re: Understanding 'Classes' and 'Objects'

Posted 25 July 2011 - 03:52 PM

View PostSimown, on 25 July 2011 - 02:47 PM, said:

Forgive me if you already understand this, I think your wording is a little fuzzy.

You model objects as real world objects, so a car would never rely on an "external 'drive' function" - the fact is that you can drive a car in the real world, so it contains that method theoretically. The car object encapsulates the methods it uses inside the object. You cannot get in a car and call a function to make the car drive now, can you?!



I suppose it was a terrible example. :sweatdrop: I guess I was trying to get the fact how class objects in themselves contain their own functions and attributes and do not have to rely on outside functions to operate. Like a car in itself can drive.


Yea...horrible example...

This post has been edited by 2DZombie: 25 July 2011 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Understanding 'Classes' and 'Objects'

Posted 25 July 2011 - 04:30 PM

That's what I was getting at, it wasn't a bad example at all - just how it was written, maybe :) Solely making sure it was clear to you!

This post has been edited by Simown: 25 July 2011 - 04:31 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1