Pythonic or not Pythonic? That is the question!

So I’ve gotten more and more used to Python as I continue to work on this final project. I am pretty proud with some individual functions considering  they’re pretty Pythonic… I think. You see I’m not 100% sure still what Pythonic means still but I think they’re Pythonic! For an example I wrote a function to edit only one line in a text file, but it requires me to write over the entire text file. Some parts of this function is pretty Pythonic to me, but over all re-writing an entire text file to just edit one line doesn’t seem very.. Pythonic.  Take a look at the function and tell me what you think!

[code]

def changeAttr(line, text):
lines = open(“character.txt”, ‘r’).readlines()
if line == 4: #if item is being changed then don’t remove all items…
if lines[4].split()[1] == ‘none’: #no item then remove whole line
lines[line] = listOfAtts[line] + “: ” + text +”\n”
else:
lines[line] = lines[line].rstrip(‘\n’) + ” ” + text + “\n” #adds item
else: #if it’s not item change entire line
lines[line] = listOfAtts[line] + “: ” + text + “\n”
out = open(“character.txt”, ‘w’)
out.writelines(lines)
out.close()

[/code]

Anyways back to my original point, I feel like you need so much experience with Python to make larger projects Pythonic as a whole. There’s so many things I do in this final project that are sort of messy because I’m not sure and don’t know if Python has slick tools to do it with so I resort back to what I know, ifs, fors, and whiles which get a bit messy, especially when nested.  I think I’m going to sign up for a Python class Junior or Senior year in order to learn more because I’m definitely hooked onto Python now. Hopefully one day I can write a program that’s 100% Pythonic.

1 thought on “Pythonic or not Pythonic? That is the question!

  1. Some good questions about what it means to be Pythonic! Learning python’s specific idioms is something that will take time and eventually, after seeing a lot of code, you will develop a sense of what “looks right” in Python.

    There are a couple features of your code snippet that could be more Unix-y, I think. First of all, the name of the file is a string literal in the function, this makes the function very single purpose. What if you later want to replace a single line in a different file? Try making the file name an argument to the function instead.

    Likewise, the literal “4” is what is often called a “magic number” in programming. I am reading your code and have no idea what is significant about “4”, and it’s likely that after a few weeks you might even forget what “4” is. Check the link for a more descriptive argument against using magic numbers!

    Check the answers to this stack overflow question for some examples of changing a line in a file. In all cases the file is read in and written completely back out. I admit, when I first had to write a similar function I was concerned that it felt inefficient to write out the entire file when I just wanted to change one line. There are two reasons why you shouldn’t worry too much about this. One is that it is generally safe to assume that Python will do “the right thing”, if you write good Pythonic code. There is often a lot of optimization going on “under the hood”. The second is that reading and writing to the file system is a very operating specific operation. In any language, even C, when you “open”, “read” and “write” a file you are really just sending requests to the operating system to perform some operation on data. The OS provides an abstraction layer between the physical bits stored on disk or in memory, and the “read” and “write” operations you want to use in your program. Generally this is a good thing, it provides a consistent API to access file-like data, without needing to worry about the details of where that data is and how it is stored. Most Operating Systems (maybe even all) don’t have any notion of “lines” built into them. That is, there is no way to tell the OS to change just line 4 of a file, because it would first have to know what a “line” is, and how to count them in a file. File data in general is just a bag of bytes, and the OS only provides operations to read/write a series of bytes. (check the man page for the “read” and “write” system calls, e.g. “man 2 write”) Any higher level structure, like lines, will only come in at the library API level.

Leave a Reply

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