Python Progress
by Vijay Kiran
So yesterday, I started reading the book Dive Into Python, and finished reading first 4 chapters. The language seems to be very interesting, somewhat similar to perl. I worked on a perl project during ’04.
So, here’s the gist of what I’ve learned:
- Python is an Object Oriented language in which everything is an Object.
- Python functions are declared using def
def some_function(param1, param2=value):
The second parameter is an optional parameter, which takes the default value “value” if we don’t specify when we are calling that function.
- Every python script or program can be called as standalone program or can be import as modules into other scripts. So the following block of a script is executed only when the module is called as a standalone program.
if __name__ == "__main__":
So this if block serves a main method for the script.
- Python’s statements end with a new line character, phew! no more semicolon typing
- In Python block’s are identified using the indentation. So bye bye braces!
- Python has “in-built” support for Lists, Dictionaries and Tuples. Lists are like ArrayList in Java, Dictionaries correspond to Hashtable in Java and Tuples are immutable lists in which the order is preserved.
- Python provides various powerful introspection methods like getattr(Object, Method) which will retrieve the reference to the method of the object.
- Python provides support for lambda functions – kind of inline methods, which can be assigned to any variable and reused anytime. This is very convenient when we want to evaluate a single expression and return some value.
Apart from Dive Into Python, I was also looking at Bruce Eckel’s Thinking in Python. Bruce says:
Python is a weakly-typed language, which means it puts the minimum possible requirements on typing. For example, you could pass and return different types from the same function
But Dive Into Python says:
Statically typed language
A language in which types are fixed at compile time. Most statically typed languages enforce this by requiring you to declare all variables with their datatypes before using them. Java and C are statically typed languages.
Dynamically typed language
A language in which types are discovered at execution time; the opposite of statically typed. VBScript and Python are dynamically typed, because they figure out what type a variable is when you first assign it a value.
Strongly typed language
A language in which types are always enforced. Java and Python are strongly typed. If you have an integer, you can’t treat it like a string without explicitly converting it.
weakly typed language
A language in which types may be ignored; the opposite of strongly typed. VBScript is weakly typed. In VBScript, you can concatenate the string ’12′ and the integer 3 to get the string ’123′, then treat that as the integer 123, all without any explicit conversion.
So Python is both dynamically typed (because it doesn’t use explicit datatype declarations) and strongly typed (because once a variable has a datatype, it actually matters).
