1) How DARE you just go and assign ANYTHING to a variable?
>>> is_totally_a_list = [1, 2, 3]
>>> is_totally_a_list = "Not a list"
No error? Really? C'mon.
2) Kind of related to point 2, what do you mean you aren't going to make getters and setters? Yeah yeah I know they kludge up the code and are just plain annoying to write, but what if you want to change the code later, without breaking compatibility?
But as time went on, I learnt a a little more about Python and am starting to see the appeal for it (It's ok Java, I still love you. That is, until C++ comes and sweeps me away.).
Regarding the above points, what I have learnt so far:
1) Not all languages are built equally. Python is a lot more dynamic that languages like Java. Instead of plain variables, Python has "names". These "names" can be tagged on to pretty much anything (and I mean ANYTHING) and they are a way to represent the memory address of an object. So a "name" acts like a little nametag on a memory 'box'. It doesn't really do anything other than point to that specific object in memory. So how is this useful? For starters, you can "name" anything things, such as function objects.
So I can actually pass in functions as arguments for other functions. One of the uses of this is a benchmarking function which compares runtimes of functions being passed in.
Ofcourse, one of the slightly disconcerting side-effects is being able to put a nametag on any type of object or datatype, anytime, anywhere (Unlike in java where you initialize a variable to hold a specific datatype, such as an int). But, once you realize how harmless the nametag is, most of the confusion dissipates.
2) This one was actually quite neat. You DO end up writing getters and setters in Python if need be. But you aren't burnt at the steak if you implement them in later iterations of your code (unlike in Java). Python has a function called "Property". What Property does is, modify the behaviour of a class variable. So everytime someone tries to access/modify it, they are actually routed through a getter/setter (the ones which you just wrote) and all the getter/setter logic is applied (Such as exception raises, etc). The biggest implication of this is that you don't need to write getters/setters if you don't presently need them. This results in much smaller and cleaner code. And if you realise you actually DO need to sanity check the assignment of a variable, you can implement the code without changing how the variable is accessed. You just need to write your getters and setters and be on your way.
So I can actually pass in functions as arguments for other functions. One of the uses of this is a benchmarking function which compares runtimes of functions being passed in.
Ofcourse, one of the slightly disconcerting side-effects is being able to put a nametag on any type of object or datatype, anytime, anywhere (Unlike in java where you initialize a variable to hold a specific datatype, such as an int). But, once you realize how harmless the nametag is, most of the confusion dissipates.
2) This one was actually quite neat. You DO end up writing getters and setters in Python if need be. But you aren't burnt at the steak if you implement them in later iterations of your code (unlike in Java). Python has a function called "Property". What Property does is, modify the behaviour of a class variable. So everytime someone tries to access/modify it, they are actually routed through a getter/setter (the ones which you just wrote) and all the getter/setter logic is applied (Such as exception raises, etc). The biggest implication of this is that you don't need to write getters/setters if you don't presently need them. This results in much smaller and cleaner code. And if you realise you actually DO need to sanity check the assignment of a variable, you can implement the code without changing how the variable is accessed. You just need to write your getters and setters and be on your way.
No comments:
Post a Comment