- #1
- 4,800
- 3,075
Hi @Mark44, I have a few comments on your recently posted Insight https://www.physicsforums.com/insights/pythons-sympy-module-and-the-cayley-hamilton-theorem/. The first two are related to the Insights platform and are directed at Greg.
To a greater or lesser degree of course these points are cosmetic, but the last two in particular really ought to be sorted as they could cause confusion.
- The code is very hard to read without syntax highlighting. @Greg Bernhardt can't we get a Prism or Highlight.js plugin?
- I think there is some horrible JavaScript running on Insights pages intercepting the copy command to insert a reference back to PF. Unfortunately this is broken (tested in FF and Chrome[ium] on Windows and Linux Mint) as it strips line endings, leaving all code on one line! I can understand the reason for wanting this but as it is implemented now it is a pain (and can easily be avoided by using the 'show source code' option in the browser and copying from there).
- It is common practice elsewhere to post code in articles as a GitHub gist: this would help work around the above problem.
- The screenshot of Idle is a great blast from the past! Seriously does anyone use Idle any more? Most Python programmers I know use VS Code, and I think many PF readers have been introduced to Python via Jupyter notebooks. Perhaps either of these coding environments would be more relevant to readers? Or just post output as text rather than a screenshot?
- There is a standard for writing docblocks which should be followed so that IDEs can parse them properly, although there are a number of variations to this standard. One way of writing the docblock for getCoeffs that would work much better could be:
Python:def getCoeffs(poly, coefficients): """ Get the coefficients of a polynomial. Parameters ---------- poly: list - a list of tuples that contain the coefficients and exponents of the polynomial in question. In the i-th tuple, the subtuple at index 1 holds the coefficient. coefficients: list - the list of coefficients that will be returned. The caller must define the list. Returns ------- coefficients: list - A list of the cofficients. """
list
is not a reserved word in Python, but it is the name of a type so using it as the name of a variable is not a good idea. You might be tempted to call it my_list but again that is not very helpful: the best name for it would becoefficients
as I have used above, however...- Passing parameters by reference in Python is not the same as passing pointers in C. To illustrate this see the following code:
Python:def passbyref(my_list): # This will change the first element of my_list. my_list[0] = 2 # This creates a new variable my_list in the scope of this function: the # reference to the original parameter is no longer available here. my_list = [3, 4] my_list[0] = 5 return my_list # Now let's see how this works: a_list = [1] b_list = passbyref(a_list); print(a_list); # [2] print(b_list); # [5, 4]
This means that your function definitions becomedef getCoeffs(poly):
anddef getExpons(poly):
and instead of:
Python:expons = [] expons = getExpons(trms, expons) # Get a list of the variable exponents of the polynomial print("Expons: ", expons)
Python:# Get a list of the variable exponents of the polynomial. exponents = getExponents(terms) print("Exponents: ", exponents)
trms
toterms
etc: there is no need for abbreviations like this and they make code harder to read.
To a greater or lesser degree of course these points are cosmetic, but the last two in particular really ought to be sorted as they could cause confusion.
Last edited: