Classes

A class in python is a data type and all data types in python are classes. Classes have always been associated with object oriented programing in many languages. Some languages are more heavy into object classes ( e.g. Java) than others C. C++ had a focus on implementing object oriented programing into the C language. As you have seen, Python treats everything in python as objects and hence can be considered a highly classfull language.

Classes in python are defined with the class statement. As a best practice in the python world classes are identified with capital letters ( as opposed to functions that are supposed to all be lower case ).


class HexConverter:
    [code]

myconv = HexConverter()
    
PEP-008

Class names naming convention PEP-0008

Class instance

After a class is defined, to use the class we instantiate it. When the program instantiates the class, it's structures and values are constructed and some execution can also be started based on internal structures. This is important to understand. For every instance that you create of the class you are defining, you are creating separate versions of the class. Each of these with unique values.


class HexConverter:
    [code]

myconv1 = HexConverter()
myconv2 = HexConverter()
    

In each instance case the values, variables and functions are unique to that instance. But you always have to reference them with the instance name in mind. In the previous example we created a instance called myconv1 = HexConverter(). If you want to reference a variable for the instance myconv1 you always reference based on that instance name. So myconv1.var1 = "Test" is never the same as var1 = "Test"

Another uniqueness to Python is that you can assign values to instantiated classes even if they haven't been pre-defined in the class.


class HexConverter:
    var1 = "Test"


t1 = HexConverter()
t1.var2 = "Test2"

print "Var 1 is:" + t1.var1
print "Var 2 is:" + t1.var2

$ python test.py
Var 1 is:Test
Var 2 is:Test2
    

In the previous example the class has defined var1 in itself. Then HexConverter is instantiated and once instantiated the variable var2 is assigned the value of "Test2" and referenced. For this reason Python classes are considered data repositories and can be utilized like this to "catch" data as needed.

Methods

A method is a function that is a member or associated to a class. There are also special methods that are part of classes. One of these is the init method. It is defined as __init__ and is executed automatically by Python when the class is instantiated.


class HexConverter:
    def __init__(self):
        self.var1 = "Test"


t1 = HexConverter()
t1.var2 = "Test2"

print "Var 1 is:" + t1.var1
print "Var 2 is:" + t1.var2

$ python test.py
Var 1 is:Test
Var 2 is:Test2
    

Similar to what we accomplished previously, we utilize the function __init__ to set the variable var1 on itself. Using what has been learned we can create a class that performs the functionality of doing HEX conversion.

Let's expand on our hex conversion example using a class. With this converter we now will create a class that is instantiated with a value and then execute the converter that based on the value entered will process and return the correct value.


import sys

class HexConverter:
    def __init__(self, value):
        if value.upper().startswith("0X"):
            self.ishex = True
            self.value = value
        else:
            self.ishex = False
            self.value = int(value, 10)


    def conv_number(self):
        if(self.ishex):
            return_value = int(self.value, 16)
        else:
            return_value = hex(self.value)

        return return_value

hc = HexConverter(sys.argv[1])
print hc.conv_number()


$ python test.py 0x15123
86307
$ python test.py 0x10
16
$ python test.py 10
0xa
    

As you can see, I defined the class with the __init__ function that based on the user input classifies the string as either HEX or INT. From there the function conv_number uses the HEX detection component to then do the proper conversion and returns the value back.