Classes 

May 25th, 2011

Since Python is a object oriented programming language then you can use things like classes to define a object, a class is a bunch of methods that act on the data that is stored within the class itself. You can store data within the class, but unlike c++/java/c# etc, there is no private/public/protected, everything is public accessible.

In the example below, the class constructor is called (__init__ method) and the first parameter is the class object itself, which is why it is there and for many reasons why it is called self as a variable name.

 
#  self means the object itself.
 
class mysimpleclass :
        __myvar = 0
        def __init__(self,value = 0) :
                self.__myvar = value
 
        def printValue(self) :
                print self.__myvar
 
simple = mysimpleclass(5)
simple.printValue()

the printValue method just does that, it will print the value that is sorted within the class object and here is the output

5

if you did not put in the self in the example above for printing out the class object variable __myvar as below

class mysimpleclass ....
       ....
       def printValue() :
            print __myvar

the error would be

NameError: global name '_mysimpleclass__myvar' is not defined

because the interrupter is looking for a global variable and not a variable attached to that object.

Add two numbers 

May 5th, 2011

To carry on my normal process of notes on this website for different languages here is the how to add two numbers with a return value from a function.

To define a function you use the “def” syntax within the programming language and then the next bit of text before the parameters for the function itself ( in this case the parameters are the (number1, number2) ), is the function name and how to call the function.

The function below takes two parameters and returns back there result of the mathematical addition, the last part is how to call the function.

def addTwo(number1, number2) : 
	return number1 + number2
 
print addTwo(1,2)

and the output would be

3

Read / Write 

May 3rd, 2011

Here is how to open and read/write to a file within Python. Most languages supply a basic file opening and read/writing methods with the closing method, and Python is no different which makes going from one language to another very easy (you just need to learn the classes that are capable to doing the interesting stuff :) ).

To write to a file, you have to open it first within a write mode (‘w’) and then write to that file *handler*, in the code below the variable ‘f’ is the file *handler*

f = open('write.txt','w');
f.write('hi there\n');
f.write('this is genux\n');
f.write('and my site is codingfriends.com\n');
f.write('pyhon does OO\n');
f.close();
print 'File has been written\n';

which will write to the “write.txt” the following text

hi there
this is genux
and my site is codingfriends.com
pyhon does OO

and then to read in the file you use the read mode ‘r’, you are able to use the Iterator style of coding with using a ‘for in :’ or if you wish to read one line at a time you can use the ‘readline()’ method, and to seek within a file you use the method ‘seek( )’.

f = open('write.txt','r');
for readInLine in f:
	print readInLine;
 
print 'seek to the start of the file';
f.seek(0);
anotherLine = f.readline();
print anotherLine;
 
f.close();

and here is the output

hi there
 
this is genux
 
and my site is codingfriends.com
 
pyhon does OO
 
seek to the start of the file
hi there

Hello world 

April 28th, 2011

Python is one of those languages that are used by allot of different companies but probably does not get the credit that how much it is used. Google uses it, allot of the Ubuntu backend is python. Python is able to do object orientated programming, but does not include the full essence of objects like other languages like C++/Java/C# where you have a private/public/protected but use a “__” before the variable as a “hidden” variable within the class.

So going to do some posts of how cool Python can be, but to start with the classic “hello world” program, like most languages (C++/PHP/C#/Java) most of the functions/methods are actually english words that should mean something to the writer/reader of the program, so to display a message to the screen you can use the “print” command and here is the example program

print "hello world"

and this will output, if you save out that program as helloworld.py

hello world

to call the saved program via the command line you need to call the interpreter (the python program).

python helloworld.py

Javascript {} [] 

March 26th, 2011

Someone asked me what is the difference between {} and [] within javascript.

Basically the difference is one is a key => value pair like PHP ‘{}’, and the other is just a pure array of values ‘[]‘.

Here is the code.

var simplearray = ["genux","linux"];
 
var keyvaluearray = {"name" : "genux", "OS" : "linux"};

To access the different values, since the simple array is index by numbers 0 onwards you use the [x] notation, and the key value array you can reference the value by the name of the key like in object orientated way.

alert(simplearray[0]);
 
alert(keyvaluearray.name);

Abstract development 

March 8th, 2011

When developing you have to think in abstract way, because otherwise you would think in a very much single way and that would make any version upgrades bad!!!.. and costly..

e.g. if you are using something like a hash map, and since it implements a interface abstract setup, then if you did some code like

Map mapy = new HashMap<String, Integer>;

and the things like iterators etc would be implemented so the rest of your code you could do something similar to this

foreach (String st in mapy)
{
....
}

but if then in the future if you want to alter to a new improved hash map lets call it the FunckyHashMap, then you just need to

Map mapy = new FunckyHashMap<String, Integer>

since all of the abstraction within both the HashMap and FunckyHashMap will both use the abstract interfaces which means that the foreach code above would still work and you only need to alter one line, e.g. very abstract in the way that things are “talked” to, e.g. the methods/functions are still present and implemented but would be working on different structures within the class/object.

The abstract way of doing things, is just the way to go!!.. create interfaces/abstract classes etc.. and so you will save yourself allot of time in the future.

Function vs Method 

March 8th, 2011

The main difference between a function and a method is that a function can live independently of the any instance of a class, where as a method sits within a class.

That is about it, e.g. a function is

int func(int value);
...
 
int main()
{
  cout << funct(3) << endl;
}

whereas a method has to live within a class

class myclass
{
    int myMethod(int it);
};
 
....
 
int main()
{
   myclass theclass;
   cout << theclass.myMethod(4) << endl;
}