[pythonvis] Re: A Question About Import And Another About List Comprehensions

  • From: "Jeffrey Thompson" <dmarc-noreply@xxxxxxxxxxxxx> (Redacted sender "jthomp" for DMARC)
  • To: <pythonvis@xxxxxxxxxxxxx>
  • Date: Wed, 1 Jul 2020 17:53:22 -0400

Hi,

  None of my comments are ever meant to be "doctrine" or "the best way", or
"the only way"
 " to do something in Python.
I don't like people who are arrogant, and I try to avoid that myself.

There are going to be times when someone gives you some unwanted advice, or
doesn't understand what you are
trying to do and why you are doing things in a certain way, and when that
happens, to me, I just ignore their advice.
Almost all the advice I have received from this email list has been great
, people are almost always rather pleasant.
I like it that way, and hope that it always stays that way.

  Jeff
----

-----Original Message-----
From: pythonvis-bounce@xxxxxxxxxxxxx <pythonvis-bounce@xxxxxxxxxxxxx> On
Behalf Of jhomme1028@xxxxxxxxx
Sent: Wednesday, July 1, 2020 4:07 PM
To: pythonvis@xxxxxxxxxxxxx
Subject: [pythonvis] Re: A Question About Import And Another About List
Comprehensions

Hi,
I am changing those variable names to lowest and sides. I thought that the
number I send to the range function should be exactly what I want to use in
sides, so that would cut down on human error. I was trying to avoid creating
a bug.

Thanks.

Jim

==========
Jim Homme
Skype: jim.homme
FreeChess: jhomme
Twitter: jimhomme
Facebook: jimhomme
Website: jimhomme.com

-----Original Message-----
From: pythonvis-bounce@xxxxxxxxxxxxx <pythonvis-bounce@xxxxxxxxxxxxx> On
Behalf Of Jeffrey Thompson (Redacted sender "jthomp" for DMARC)
Sent: Wednesday, July 1, 2020 2:21 PM
To: pythonvis@xxxxxxxxxxxxx
Subject: [pythonvis] Re: A Question About Import And Another About List
Comprehensions

Hi Jim,

Congratulations on doing your first list comprehension!

  1) The Python style guides says to use the straightforward import
statement rather than the form:
from aModule import anItem

This avoids name collisions as you pointed out, which can be a possible
maintenance issue if someone is not aware of your import statement.
That is why most people put their import statements at the top of their
programs, so that they will be obvious to any experienced programmer who
understands the problem of name collisions with import statements.
If The program is rather short, a problem is less likely to develop,

By the way, if you are only using the imported module in a single function,
and you may not even call that function,  then you can insert the import
statement at the beginning of the function, and the import statement will
only be executed when the function is first called.
Python only imports a given module just once in any program, so re-importing
the module will not cause problems.

Also,, if you are going to use the "from" version of the import statement,
and you really don't want to type much, then you can use the "as" keyword to
give the imported item a shorter name, as in:
from a_module import a_really_really_long_name as arrln

and then in your code, you would type "arrln" instead of
"a_really_really_long_name"

This does not help readability much, though,  especially if it is not
explained close to where it is being used later on in your program.

However you can produce the same results by putting the statement:
arrln = a_module.a_really_really_long_name

close to where you are going to use it in your program.

2)
You have the right idea, and this will work.
I have a few suggestions:
1) You need a carriage return to put your print statement on a separate
line.
2) "min" and "max" are Python built-in functions.
You should probably use "the_min" and "the_max" or something similar to
avoid name collision and any possible confusion.
3) You use the value of max in your input to randint() and your range()
input.
In practice, these are likely to be separate values, and maybe should be
assigned separately, instead of using the same value for both.

  Otherwise, it's very good, especially for a first time at doing list
comprehensions.

  Jeff Thompson
-------

-----Original Message-----
From: pythonvis-bounce@xxxxxxxxxxxxx <pythonvis-bounce@xxxxxxxxxxxxx> On
Behalf Of jhomme1028@xxxxxxxxx
Sent: Wednesday, July 1, 2020 1:15 PM
To: pythonvis@xxxxxxxxxxxxx
Subject: [pythonvis] A Question About Import And Another About List
Comprehensions

Hi,
As I think about using import, I feel like even if it's overkill, you should
very often import something and then write dot notation to explicitly tell
Python to use a module or variable to keep from accidentally using one from
your program. But I still want to ask which form of the import statement you
should use and when. So, what is the best practice for this?

Now to list comprehensions. I am proud that I understood one and got it to
work, but I feel that sometimes I might want to favor using a for loop if it
makes the code more readable. What is the best practice for this. Below is
my code for getting and rolling the initial six dice in Farkle.

And thanks for your answers in advance.

import random

min = 1
max = 6

table = [random.randint(min, max) for i in range(max)] print (table)


Thanks.

Jim

==========
Jim Homme
Skype: jim.homme
FreeChess: jhomme
Twitter: jimhomme <https://twitter.com/jimhome>
Facebook: jimhomme
Website: jimhomme.com <https://www.jimhomme.com



List web page is
//www.freelists.org/webpage/pythonvis

To unsubscribe, send email to
pythonvis-request@xxxxxxxxxxxxx with "unsubscribe" in the Subject field.

List web page is
//www.freelists.org/webpage/pythonvis

To unsubscribe, send email to
pythonvis-request@xxxxxxxxxxxxx with "unsubscribe" in the Subject field.

List web page is
//www.freelists.org/webpage/pythonvis

To unsubscribe, send email to
pythonvis-request@xxxxxxxxxxxxx with "unsubscribe" in the Subject field.

List web page is 
//www.freelists.org/webpage/pythonvis

To unsubscribe, send email to 
pythonvis-request@xxxxxxxxxxxxx with "unsubscribe" in the Subject field.

Other related posts: