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!
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.split() == ‘none’: #no item then remove whole line
lines[line] = listOfAtts[line] + “: ” + text +”\n”
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’)
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.