By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

Simple "Class" explaination

P: 32
Hi,

Although I have been programming in python for years ... I rarely use
classes .... just functions etc. .... anyway I was wondering if there is
someone who could give me a simple way of remembering/understanding
what a class is .... and when it should be used.

Thanks,
Mar 28 '08 #1
Share this Question
Share on Google+
2 Replies


micmast
100+
P: 144
Wikipedia:
A class is a cohesive package that consists of a particular kind of metadata. It describes the rules by which objects behave; these objects are referred to as instances of that class. A class has both an interface and a structure. The interface describes how the class and its instances can be interacted with via methods, while the structure describes how the data is partitioned into fields within an instance. A class is the most specific type of an object in relation to a specific layer. A class may also have a representation (metaobject) at runtime, which provides runtime support for manipulating the class-related metadata.
Maybe an example :)
Let's take a look at a car, a car has 4 wheels, 3-5 doors, a certain type of fuel, a brand, engine, bhp, kw,...
So we could say that "car" is just a package name for everything with 4 wheels, 3-5 doors,...
So this could be made with a class

Expand|Select|Wrap|Line Numbers
  1. class Car:
  2.  
  3.     doors = 3
  4.     wheels= 4
  5.     bhp=115
  6.     kw=81
  7.     engine=1900
  8.     brand="volkswagen"
  9.     fuel="petrol"
  10.  
  11.     def __init__(self):
  12.         #This method is called when a car object is made
  13.  
  14.     def __init__(self,doors,wheels,bhp,kw,engine,brand,fuel):
  15.         self.doors = doors
  16.         self.wheels = wheels
  17.         self.bhp = bhp
  18.         self.kw = kw
  19.         self.engine = engine
  20.         self.brand = brand
  21.         self.fuel = fuel
  22.  
  23.     def implement_more_stuff(self):
  24.         return None
  25.  
(The code might not be correct but it's about the point right?)

anyway this is how you use it

Expand|Select|Wrap|Line Numbers
  1. >>>import classexample
  2. >>>newcar = classexample.Car()
  3. >>>newcar2 = classexample.Car(3,4,200,100,2500,"audi","petrol")
  4. >>>newcar2.implement_more_stuff()
  5. None
  6. >>>newcar.implement_more_stuff()
  7. None
  8.  
ok this might seem odd, but what you did was create 2 cars, these cars can have other values in the variables and they can exist next to each other. And yet we only have to import 1 python file.
That is a class in a nutshell
Mar 28 '08 #2

P: 32
Thanks ... this has been a great help.
Mar 30 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.