Simple "Class" explaination

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fordie1000
    New Member
    • Mar 2008
    • 32

    #1

    Simple "Class" explaination

    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,
  • micmast
    New Member
    • Mar 2008
    • 144

    #2
    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

    [code=python]
    class Car:

    doors = 3
    wheels= 4
    bhp=115
    kw=81
    engine=1900
    brand="volkswag en"
    fuel="petrol"

    def __init__(self):
    #This method is called when a car object is made

    def __init__(self,d oors,wheels,bhp ,kw,engine,bran d,fuel):
    self.doors = doors
    self.wheels = wheels
    self.bhp = bhp
    self.kw = kw
    self.engine = engine
    self.brand = brand
    self.fuel = fuel

    def implement_more_ stuff(self):
    return None
    [/code]
    (The code might not be correct but it's about the point right?)

    anyway this is how you use it

    [code=python]
    >>>import classexample
    >>>newcar = classexample.Ca r()
    >>>newcar2 = classexample.Ca r(3,4,200,100,2 500,"audi","pet rol")
    >>>newcar2.impl ement_more_stuf f()
    None
    >>>newcar.imple ment_more_stuff ()
    None
    [/code]

    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

    Comment

    • fordie1000
      New Member
      • Mar 2008
      • 32

      #3
      Thanks ... this has been a great help.

      Comment

      Working...