Programming: Issue #3 [Classes][Python]: Basic Classes

The next step on the socket programming would be to create a server that can handle many connections at the same time. In order to do this, we’ll need to have multiple threads running on the server program. However, before moving onto threads, I think it can prove useful to explain the main concepts behind the implementation of classes.

This first example shows one of the minimal classes with a ‘real’ fucntionality one can create on Python:

This does nothing useful, but it’s a simple example to show a class creator and a function on it, and it’s good for the purpose how classes on python work by default.

– __init__ is the keywork for the class creator. To create a class object from the main program we’ll be using the x = ClassTest(n) command, where x refers to the name of the object we’re creating, and n is the value we pass to the constructor.

– printCT() is a function of the class that we can call on one of the objects we have created, that returns the value of the i variable inside the object. In this case, it does the same as calling x.i, since the i value on this object can be allowed directly from outside the object.

As an execution example:

———-
x = ClassTest(10)
y = Classtest(15)
print x.printCT() + x.i
———-

Would print on screen ’25’.

There is a problem with this with programmers coming from other languages such as C++ as myself. The class doesn’t have a section that specifies it’s atributtes. This is because python classes are mutable, all of them have a hidden dictionary (try printing ‘x.__dict__’!) that points to every class attribute we add. These attributes can be deleted aswell using the ‘del’ function. We can do that from outside the class with a ‘del x.i’ or from itself with a ‘del self.i’.

Example:
———-
x = ClassTest(15)
del x.i
print x.printCT()
>>> AttributeError: ClassTest instance has no attribute ‘i’
———-

Data Hiding:

On most cases we don’t want an attribute to be accessed from outside the class. This is when data hiding comes in. This example is the same as the first one, but we won’t be able to declare a x object and cast x.i. To hide a variable, we just have to add ‘__’ before it:

———-
x = ClassTest2(10)
print x.printCT()
>>> 10
print x.i
>>> AttributeError: ClassTest instance has no attribute ‘i’
———-

However, this attribute is not totally blocked from outside access. This is just a way to help on Object Oriented programming. Remember I said every class has a hidden __dict__ attribute on it? Let’s exploit that:

———-
x = ClassTest2(10)
print x.__dict__
>>> {‘_ClassTest__i’: 10}
———-

Voilà. We now know how to access that variable:

———-
x = ClassTest2(10)
print x._ClassTest__i
>>> 10
———-

This is just for learning purposes. The fact that you can still access that variable from outside doesn’t mean you have/want to do it, we’re doing this to avoid outside access.

Unmutable classes:

Having a dict attribute to enable mutability comes at cost of having unefficient code (you can’t have it all). Sometimes we need to have a class more focused on being static and saving resources, since we’re going to have a lot of instances of that class and every byte counts.

To do this, we have the __slots__ keyword.

Now this is a more complex and useful (sort of…) class.

– Note that I’ve changed the class to ‘ClassTest(object)’. This is required for any class you plan to use in a real program. While I don’t know exactly everything the object inheritance does, I can tell that it’s at very least required for __slots__ to work. If the object class is not included, __slots__ does nothing.

– I’ve now defined setters and getters functions for the only attribute of the class, since it is private. Unless we declare these functions, there is ‘no way‘ to access or edit those values.

– Slots can be used to define non-hidden values, we could implement a ‘__slots__ = [“__i”, “j”]’ and one of the attributes would be private and the other wouldn’t.

– This class cannot get more variables, neither by the usage of a “x.j = n” or “self.j = n”, it is limited to the variables defined on the slots sentence.

– Using __slots__ destroys the __dict__ structure since it’s not longer needed.

It seems like __slots__ gives problems when using inheritance between objects. I’ve readed should be used only as a way to save time/memory on very specific programs, not as a way to make a class unable to mutate, so my advice is to use it or not depending on personal preference (a lot of information about this can be found on a single google query, if you really require to know this on detail) as long as it doesn’t throw errors.

This is everything regarding basic class implementation by now. I’m actually uploading the .py files to MediaFire and I expect to have them linked here in a brief moment.

Issue 3 files

This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a comment