Taylor's Blog

Atypical ramblings

Happy New Year!

Wow. It’s the end of 2015. What a ride it has been. I can’t believe that I started programming a year ago. It feels… not so long ago haha. Looking back, I see that I have really grown as a developer. And I guess as a person too. I can’t continue this post without first mentioning the biggest thing to happen in my life. Last week, December 21st, I got married to the most wonderful girl in the world. I really couldn’t have come so far without her never-ending support. I really am blessed. Because she was on a fiance visa, we only did the civil wedding at city hall. A really low-key thing. It was great though – my whole immediate family got to be there. We plan on doing a much grandeur wedding this April/May when her mother comes to visit. How many people get to say they had two wedding celebrations, huh?

Last month I finished my first computer science class – Introduction to Computer Science I. It’s only the first step of my academic journey, but I can’t help comparing this semester to the semester I had ten years ago. Ten years ago I was a freshman in college also enrolled  in my first CS class at the UMass: Dartmouth. I was taking Object Oriented Programming I and I had never felt more in-over-my-head than those few weeks I was in the class. I was thrown right into a Linux environment and expected to do… well pretty much the same stuff I did this semester at OSU. I realize now that at that point in my life, I wasn’t ready for the rigors of pursuing a CS degree. I was naive and immature. In a panic, I switched out of the program – eventually winding up in the history department after a semester of non-major pre-requisite classes. Looking back, I see that switching out was the right thing to do. Had I stayed in that class, I probably would have flunked it. Maybe I would have even flunked out of college. It was a scary time.

But now all that is behind me. I have a bright future ahead of me and I’m feeling exceedingly optimistic. I love the OSU program so far and feel like my knowledge is growing exponentially each day. Second semester starts next Monday. In the meantime, I’m back to learning Python and Flask. I really want to deploy my program onto the web. I’m very excited about it. However, I’ve been away from Python so long that I’ve forgotten a lot of its conventions. While doing some simple programming, I had a devil of a time getting a program to run correctly until I remembered that you can’t use the ++ operator in Python. As Steve says, I was “corrupted by C++”, heh.

Well let’s talk about some new stuff. What have I learned recently?

  • If you put a main.py file and a module file in the same directory and run the main.py file, it’s able to import from the modules without an __init__.py file. However, if the module was in another directory, you would need to add an empty  __init__.py file. This is because the __init__.py file is how Python identifies directories from which you’re allowed to import. The directory of the script you’re running is an exception – you’re always allowed to import from it. (source)
  • When the Python interpreter reads a source file, it executes all of the code found in it. Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to the module’s name. (source)

Here is a (relatively) simple example of how to import a class, create an object and use a method in that object:

#main.py
from myModule import MyClass

def main():
    instance = MyClass()
    instance.myMethod()

if __name__ == "__main__":
    main()
#myModule.py
class MyClass(object):

    def __init__(self):
        print("Object created!")

    def myMethod(self):
        print("Hello from myClass.myMethod()")

When run, this will be the result:

Object created!
Hello from myClass.myMethod()
Updated: January 5, 2016 — 1:53 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

Taylor's Blog © 2015