When I'm writing code for a personal project, or if I'm just testing things out, I tend to code like this, just because it makes me happy:
def importcontacts(request):
context = initialize_context(request)
context['form'] = UploadedFileForm()
token = get_token(request)
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
contacts = request.FILES['file']
fs = FileSystemStorage()
filename = fs.save('import_data.json', contacts)
uploaded_file_url = fs.url(filename)
context['fails'] = ct.import_contacts(uploaded_file_url,
token,
context['user']['email'],
context['user']['name'])
messages.success(request, 'Contacts imported successfully.')
return render(request, 'contactsdb/importresult.html', context)
return render(request, 'contactsdb/import.html', context)
Obviously this isn't in any way PEP8 compliant and I would never put something like this into production but at the same time I don't truly know why and I also don't truly understand why the code even still works when set out like this. I assume all the space makes for slower code?
Googling has not helped me find my answer. I'm not looking for someone to tell me "you should never do that, blah blah", I'm well aware! I'd just like to know the reasons as to why this isn't OK.
This was an interesting question. Basically the use of white spaces between the operands and the operator is to increase readability. It's just a matter of personal choice to add one white space or add ten. The interpreter/compiler doesn't care about white spaces. It's just about readability.
Now when you do something like this-
a = 10
b = 100
c = a + b
And when you do something like this-
a = 10
b = 100
c = a + b
You'll notice that the first one is more readable than the second one, but this doesn't mean that the second one is wrong. It is just a matter of personal choice. Basically the convention says to follow the first method but we would get the same output with or without white spaces.
So you may use one or ten white spaces in your program, nobody can question you!!