473,467 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple "Class" explaination

32 New Member
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
2 1624
micmast
144 New Member
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
fordie1000
32 New Member
Thanks ... this has been a great help.
Mar 30 '08 #3

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

Similar topics

5
by: Richard Barnet | last post by:
I'm trying to style a line break, something like this: <br class="spacer" /> via CSS, but none of the following seems to have any effect. Any ideas? br.spacer { clear: both; height: 50px;...
6
by: dpr | last post by:
I have come accross a piece of C++ code with the construct: MyClass *c = new class MyClass(); Is there a difference between this and: MyClass *c = new MyClass(); ?
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
8
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
9
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
1
by: BobPaul | last post by:
I'm following code out of a howto book and this is really bugging me. This header file was created by VStudio 6.0 when I did a "Right Click: Add Member Function" CLine is a class I wrote (per the...
3
by: Matt | last post by:
Hi everyone, can someone point out the difference between defining a class with the public access modifier and just leaving it as it is (default in vs.net "class") I think that the default...
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
7
by: BobBlock | last post by:
I have a box defined like this: <div id="shadow-container1"> <div class="shadow1"> <div class="shadow2"> <div class="shadow3"> <div class="container"> the innermost "container" is where...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.