In Python, as in many other programming languages such as Java, C, and etc., object-oriented programming is a fundamental characteristic of the programming language. Objects are created by initiating an instance of an class, with a set of data known as attributes that consists of data types such as int, float, boolean, string, and etc. As well, object have a set of defined methods called functions that acts upon the attributes of an object. Functions can be used to modify, manipulate, or return the data of an object. The data of an object is typically created when a class is initiated. However, it should be noted that in Python, the data of an object can be set outside of its parameters such as in the console. There is not such thing as private/public variables as is the case in Java, though changing the attributes of an object outside of its parameters as defined in its class is in general considered bad coding practice. In Python, every abstract data type is an object.
Object-oritented programming utilizes a concept known as encapsulation, whereby the code of an object is "hidden" from other parts of the code, such that the modification of the code of one object does not interfere with the code of another object. By "partitioning" the code into separate sections, code organization is improved. Through this method of "information hiding", programmers need not fear that changing one section of their code can lead to unexpected changes in another section of their code.
To better understand object-oriented programming, we can look at some real world equivalents of an object. Take for example the vending machine. The vending machine has a set of internal mechanisms that allows it to dispense soft drinks. However, the user of a vending machine does not need to understand the inner mechanisms behind the vending machine in order to use it; the user simply needs to understand the interface of the object to operate the machine. The designer can tinker with the internals of the vending machine and implement a new design for it, but so long as the interface remains the same (or at least similar enough such that the user is still familiar with how to operate the machine), this will not pose a problem. As well, if the designer tinkers with the internals of a single instance of a vending machine, this will not lead to system-wide changes in all vending machines in the world. This same sort of logic applies with object-oriented programming. The implementation of an object can be hidden from the user; the user simply needs to know what the object does in order to use it. We can change the internals of a single object and not expect this to lead to changes in other objects that exists in our code.
For those that are still confused about how object-oriented programming works, I found that the entry on Wikipedia was quite useful to in better understanding the concept of object orientation. The reading is informative, relatively concise, descriptive and explained in a language that is simple to understand. Of course, the best way to understand object-oriented programming is to actually use it in practice, though having some background knowledge in the matter never hurts ones understanding.
Link: http://en.wikipedia.org/wiki/Object-oriented_programming#Overview
Please feel free to make any comments or suggest any changes to the entry below! I'm quite curious as to what sort of sentiments other people in CSC148 feel with regards to Object-Oriented Programming, as I myself find it to be a much more "pleasing" method of programming that appeals to my need for organization and code management. Let me know below in the comments! As well, a note for fellow classmates in CSC148: feel free to link my posts to your blog if you wish to do so (for whatever reasons). Cheers!
No comments:
Post a Comment