Sunday, August 17, 2014

Python Classes and the__init__ method (not the _init_ method)

Can you tell the difference between "init"s in the title? It's hard to see and in some cases, impossible.

I was doing an exercise on Codecademy and it failed to mention one teeny, tiny thing. When you write the function/method/constructor "__init__" inside a class, you must use double underscores, not single. Otherwise, when you try to initialize your object, you'll get an error message.

On Codecademy, you can't tell the difference between single and double underscores by looking at the screen. Only because of StackOverflow did I figure out the problem. If you don't believe StackOverflow, check out MIT's Python MOOC as well.

So what is __init__? Codecademy refers to it  as a function and someone in StackOverflow differentiated between init as a method and a constructor. Call it whatever you want. In the Python documentation, it's referred to as a "special method" which initializes the object. In C#, that would be a constructor. 

Unfortunately, I could not find any other documentation to highlight the fact that double underscores are required. If you search for this problem, you'll see many people have been hindered by this issue. For now, I'll accept that this is the way it is and I'll append a note to this post if I ever find anything official on the matter.

Saturday, August 2, 2014

Python, floating points and range()

MIT's MOOC, "A Gentle Introduction to Programming Using Python" has just taught me that Python's range() does not like integers. Actually, it didn't teach me this at all. The reading discussed getting a range of random floating points and then testing that function to determine how many times each random number occurred. The reading didn't say anything about not being able to use range() with floating points. I figured that out when I tried it myself and it didn't work.

Then, as usual, I hopped over to StackOverflow which was full of suggestions that I perceived to be too unduly complex for the reading. Plus, most of the solutions involved integers in range(). This is precisely what I was trying to avoid.

I am completely new to Python but I continue with my mission of making the unnecessarily complex discernable to newbies. Keep that in mind when you read my solution below.

The reading I refer to is here. I don't use those examples. Here are mine:

#first, get some random numbers
import random #kindof like "using" in C#
randList = [] # we'll need this list soon; make it empty for now
def randomness:
          hi = 2.0
          lo = 1.0
          for i in range(10):   #this will print out 10 random numbers
                 x = random.uniform(lo, hi)
                 #generates random floating point between the lo and hi range
                 randList.append(x) # this adds the value in x to the list
           print randList
               
#test randomness() to see how many times each range of random numbers occur
def test(randList):
       count = 0
       for num in randList:
              if num < 1.5: #I want to see how many numbers less than 1.5 are generated
                     count += 1
       print ("There are/is"), count, ("occurrences of floating points less than 1.5.")

Call randomness() and then call test().

I will also post this in GitHub. More info on range() here.