понедельник, 23 февраля 2015 г.

"OOP" or "How ++ can matter"

Well, Python has OOP in it from the beginning, so let's forget the ++. Object-oriented programming is the topic of many debates, which are not the main objective of our (students) learning, so we leave those for experts and proceed with given: OOP can help (and/or allow) do things in a better, more understandable, more efficient way. People who've had some programming experience always mention OOP in a context that it is the proper, sterling way to code, which it, in many cases, indeed is. When I was working on assignment №1, I wasn't thinking how I could implement that without OOP, now that I think about it, it seems not very feasible, at least, reasonably: I'm pretty sure you can find a person who can write the same thing in ASM or in machine code, but readability and ease of use will, of course suffer. In other words, why complicate if you can simplify? Making use of "objects" which have "properties", "methods" and "attributes" - looks like a very vague idea, but you can specify each subtle aspect of the object you've created - what it looks like if you call print on it, what defines the object, how it interacts with other objects and so on. In my opinion, OOP is the further development of the idea of a variable: variables can have text and/or numbers in them, or lists of the above, can be manipulated, created, and so on. Now our "variable" (an object of type class) can itself interact using "smaller" interactions as building bricks, the "smaller" interactions being the Python itself. I'm pretty sure that I'm far from describing OOP in the best manner possible, but this is how I see it - creating building blocks that have many functions besides being a building block, just like a Swiss army knife which you tailor according to your needs.
However, there's some conventionality and formality that I don't seem to get: why would we be using a method placeholder that raises the exception when we can implement basics in it and use them later on in subclasses? Maybe it's that thing that one should remember and do, but I always try to find a reasoning, and, so far, I haven't been able to find one.

среда, 18 февраля 2015 г.

Recursion: first look

This time I'm describing my experience with recursive functions: writing, tracing in order, finding out what a function does. Generally, recursion is considered as a concept that is difficult to understand. However, this is not exactly true. It is true that "thinking recursively" is a skill that has to be practiced a lot in order to be mastered, but it does not involve anything beyond common sense and basic knowledge of programming. In short, recursion is an algorithm whereby a function calls itself. Once you're comfortable with how a function "sees" the data tha you pass to it and know what it does with a smallest subunit of data (e.g. a list), you can easily trace a call in order. So, tracing is relatively easy, unlike writing recursive functions. Sometimes it's difficult to find the right place for a recursion statement (i.e. when a function should call itself), sometimes it's challenging to squeeze the actual functionality in (i.e. where the function changes or creates its output). Several times I'd find myself having a function that has recursivity, that goes through the input the right way, but produces no output. Creating a variable that will be able to change itself depending on its value in "daughter" functions - this is what is tricky for me. At least, I don't breeze through this part every time, some functions and algorithms are easier to write than others.
To sum up, the whole concept of recursion is of high importance and can be applied anywhere you need (or don't need, just want) it. Yet, with high importance increased difficulty comes. A bad thing? Expectable, not more than that.

вторник, 3 февраля 2015 г.

First weeks...

So far, my journey to the fascinating world of Python's going fine. It feels that the language can allow you to do lots of things, even shooting yourself in the leg, if you know how to make Python do so. Every object seems to have "hidden" properties that return an error unless you've done something to them to make them work the way you want them to. One good example'd be class method "abstractmethods" - it is in object's dir, but returns an error upon calling help on that. However, this thing can be modified and used the way you want it to. In the context of the course, this is not the most important thing, though.
Recursion, apparently, opens a whole new field in Python. Looks exploitable, however, caution should be used when running some recursion-containing function. Otherwise, you're very likely to run into an infinite loop. Fortunately, this won't be too bad - most CPU's have multiple cores, Pythin uses only 1, so the freeze won't be awful. Moreover, Python seems to have a built-in loop detector, which quits a program if a loop's been run over 300 times.
Messing with attributes of classes and making them inaccessible for your typical user also seems to be a powerful tool, especially for managing objects that are not supposed to be modified. It might be difficult to appreciate usefulness of these small things until you run into a situation where you absolutely have to make use of them. What is knowing a programming language if not knowing small useful things about it?
Looking forward to the upcoming lecture on sorting mechanisms - I'd like to use a faster algorithm in one of the scripts I wrote to make my life easier, as the default, built-in sorting algorithm in Python is extremely slow.