Introspection in Python

I just finished reading a chapter about Introspection in the book that I’m reading. Learning the concept of introspection and doing it in Python is the trickiest challenge I’ve come across so far. Introspection, by definition, is determining and manipulating objects at runtime. The idea is that we know that there are objects that will definitely come across our program during run-time. We don’t know what they are exactly, but we want to do something about them in case our program encounters them....

November 3, 2010 · 8 min · 1583 words

Python and its weird boolean logic

I found some pretty interesting keywords in Python: and and or. Coming from a C background, this is somehow new to me since I’m used to using symbolic operators for boolean logic (&& and ||). One of the things I noticed was how Python interprets these keywords: 1 'a' and 'b' It returns ‘b’. Surprised? I know I am. One look and I immediately thought it would return something similar to True since both values aren’t null....

November 2, 2010 · 2 min · 238 words

Python and its different way of type casting

I tried to make a Fibonacci program in Python to experiment with getting user input, displaying output and type casting. This was the code that I came up with at first: 1 2 3 4 5 6 7 8 9 10 11 def fib(n): print 'n =', n if n > 1: return fib(n - 1) + fib(n - 2) else: print 'end of the line' return 1 n = raw_input('Input number: ') int(n) fib(n) When I tried running the program in the CLI, I have been successful in getting the user input....

November 1, 2010 · 3 min · 573 words

Optional Arguments, a gift from heaven

I’ve been messing around with functions in Python. One neat feature that I came across are optional arguments. Here’s some code that I’ve written to explain more about optional arguments: 1 2 3 4 5 def foo(length, bar = 5, foobar = 10): """A sample function demonstrating optional arguments. Takes 3 integer objects as its arguments""" print "Length is %d\nBar is %d\nFoobar is %d\n" % (length, bar, foobar) As with any other language out there, you can set default values for certain arguments in case they aren’t mentioned in a function call....

November 1, 2010 · 2 min · 232 words

The shortest 'Hello World' ever

I decided to start learning Python recently. I figured I should try learning something new outside the C-family of languages (C, C++, Java, C#, etc) just for kicks. Python seems like the perfect language to make that transition that I want since it is somewhere between C and non-C in terms of syntax. Since I have no idea how to start learning Python, I decided to ask a question on Programmers on Stack Exchange, which is a Q&A site that I visit most of the time....

November 1, 2010 · 2 min · 298 words