New to Python - block grouping (spaces)

BartC bc at freeuk.com
Mon Apr 20 14:05:47 CEST 2015


On 20/04/2015 11:30, Steven D'Aprano wrote:
> On Monday 20 April 2015 12:43, Rustom Mody wrote:
>
>> You've a 10-file python project in which you want to replace function 'f'
>> by function 'longname'
>> How easy is it?
>
> About a thousand times easier than the corresponding situation:
>
> You have ten PDF files in which you want to replace the word "f" with the
> word "longname".

Why would the PDF example be particularly difficult? (I assume they can 
simply be processed individually.)

Some tool would be used to edit the textual contents of the files, and, 
in the absence of any more specific instructions (there being embedded 
code examples for instance, or some literal uses of "f", or it's verse 
that still needs to rhyme), then you just replace each whole-word "f" 
with "longname".

(It might well be 1000 times harder if the PDFs contained full-featured 
Postscript where all the "f"s are generated at runtime, or their glyphs 
are formed with highly elaborate graphics.)

> You have identified a weakness in the current Python ecosystem. Our
> automated refactoring tools are currently quite weak and primitive.

I'm sure everyone is already aware of the difficulties with doing this 
with Python code, but apart from comments and strings, there are 
examples such as this:

if cond:
	def f():
		print ("This is F")
else:
	f=3.142
x=f

In this last line, it's not possible to tell whether f refers to the 
function, or the number. (I assume the requirement is to change calls to 
f(), or any references to the name, to longname(), as well as the defs.)

Even harder is:

  eval(s)

where the string s may also contain references to f.

There is also code that may be commented out temporarily that you want 
to change, and other comments that you don't want to change. Etc.

With some languages the task is simpler, as what is or isn't a valid 
reference to "f" can be determined by examining the source code. 
(Although C gives Python a run for its money by being able to use its 
macro system to make certain code impossible to analyse from the static 
sources.)

-- 
Bartc



More information about the Python-list mailing list