Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77853 > unrolled thread

Shuffle

Started bySeymore4Head <Seymore4Head@Hotmail.invalid>
First post2014-09-13 19:47 -0400
Last post2014-09-15 16:30 +0200
Articles 7 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Shuffle Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-13 19:47 -0400
    Re: Shuffle Chris Angelico <rosuav@gmail.com> - 2014-09-14 10:28 +1000
    Re: Shuffle Michael Torrie <torriem@gmail.com> - 2014-09-13 19:32 -0600
      Re: Shuffle Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-13 22:10 -0400
    Re: Shuffle Dave Angel <davea@davea.name> - 2014-09-15 09:32 -0400
      Re: Shuffle Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-16 00:02 +1000
        __import__(name, fromlist=...), was Re: Shuffle Peter Otten <__peter__@web.de> - 2014-09-15 16:30 +0200

#77853 — Shuffle

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-09-13 19:47 -0400
SubjectShuffle
Message-ID<ruk91al1vliuf3uobq1e0el2hrp61p6uf5@4ax.com>
Here is a screenshot of me trying Dave Briccetti's quiz program from
the shell and it (the shuffle command) works.
https://www.youtube.com/watch?v=VR-yNEpGk3g
http://i.imgur.com/vlpVa5i.jpg

Two questions
If you import random, do you need to "from random import shuffle"?

Why does shuffle work from the command line and not when I add it to
this program?

import random
import shuffle
nums=list(range(1,11))
shuffle(nums)
print (nums)

I get:
No module named 'shuffle'

[toc] | [next] | [standalone]


#77854

FromChris Angelico <rosuav@gmail.com>
Date2014-09-14 10:28 +1000
Message-ID<mailman.14002.1410654536.18130.python-list@python.org>
In reply to#77853
On Sun, Sep 14, 2014 at 9:47 AM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> Two questions
> If you import random, do you need to "from random import shuffle"?
>
> Why does shuffle work from the command line and not when I add it to
> this program?
>
> import random
> import shuffle

To understand this, you need to understand what the 'from' import does:

https://docs.python.org/3/tutorial/modules.html#more-on-modules

It's not the same as "import shuffle", but is a variant of "import random".

ChrisA

[toc] | [prev] | [next] | [standalone]


#77856

FromMichael Torrie <torriem@gmail.com>
Date2014-09-13 19:32 -0600
Message-ID<mailman.14003.1410658396.18130.python-list@python.org>
In reply to#77853
On 09/13/2014 05:47 PM, Seymore4Head wrote:
> Here is a screenshot of me trying Dave Briccetti's quiz program from
> the shell and it (the shuffle command) works.
> https://www.youtube.com/watch?v=VR-yNEpGk3g
> http://i.imgur.com/vlpVa5i.jpg
> 
> Two questions
> If you import random, do you need to "from random import shuffle"?
> 
> Why does shuffle work from the command line and not when I add it to
> this program?
> 
> import random
> import shuffle
> nums=list(range(1,11))
> shuffle(nums)
> print (nums)
> 
> I get:
> No module named 'shuffle'

You can do it two ways:
Refer to it as random.shuffle()

or

from random import shuffle

I tend to use the first method (random.shuffle).  That way it prevents
my local namespace from getting polluted with random symbols imported
from modules.

[toc] | [prev] | [next] | [standalone]


#77857

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-09-13 22:10 -0400
Message-ID<n1u91atu3gk9md07vo1o2e7apf1951a8kj@4ax.com>
In reply to#77856
On Sat, 13 Sep 2014 19:32:55 -0600, Michael Torrie <torriem@gmail.com>
wrote:

>On 09/13/2014 05:47 PM, Seymore4Head wrote:
>> Here is a screenshot of me trying Dave Briccetti's quiz program from
>> the shell and it (the shuffle command) works.
>> https://www.youtube.com/watch?v=VR-yNEpGk3g
>> http://i.imgur.com/vlpVa5i.jpg
>> 
>> Two questions
>> If you import random, do you need to "from random import shuffle"?
>> 
>> Why does shuffle work from the command line and not when I add it to
>> this program?
>> 
>> import random
>> import shuffle
>> nums=list(range(1,11))
>> shuffle(nums)
>> print (nums)
>> 
>> I get:
>> No module named 'shuffle'
>
>You can do it two ways:
>Refer to it as random.shuffle()
>
>or
>
>from random import shuffle
>
>I tend to use the first method (random.shuffle).  That way it prevents
>my local namespace from getting polluted with random symbols imported
>from modules.

Thanks.

[toc] | [prev] | [next] | [standalone]


#77887

FromDave Angel <davea@davea.name>
Date2014-09-15 09:32 -0400
Message-ID<mailman.14027.1410788105.18130.python-list@python.org>
In reply to#77853
Michael Torrie <torriem@gmail.com> Wrote in message:
> On 09/13/2014 05:47 PM, Seymore4Head wrote:
>> Here is a screenshot of me trying Dave Briccetti's quiz program from
>> the shell and it (the shuffle command) works.
>> https://www.youtube.com/watch?v=VR-yNEpGk3g
>> http://i.imgur.com/vlpVa5i.jpg
>> 
>> Two questions
>> If you import random, do you need to "from random import shuffle"?
>> 
>> Why does shuffle work from the command line and not when I add it to
>> this program?
>> 
>> import random
>> import shuffle
>> nums=list(range(1,11))
>> shuffle(nums)
>> print (nums)
>> 
>> I get:
>> No module named 'shuffle'
> 
> You can do it two ways:
> Refer to it as random.shuffle()
> 
> or
> 
> from random import shuffle
> 
> I tend to use the first method (random.shuffle).  That way it prevents
> my local namespace from getting polluted with random symbols imported
> from modules.
> 
> 

Or a third way:

import random
shuffle = random.shuffle


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#77888

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-09-16 00:02 +1000
Message-ID<5416f15e$0$30000$c3e8da3$5496439d@news.astraweb.com>
In reply to#77887
Dave Angel wrote:

> Michael Torrie <torriem@gmail.com> Wrote in message:

>> You can do it two ways:
>> Refer to it as random.shuffle()
>> 
>> or
>> 
>> from random import shuffle
>> 
>> I tend to use the first method (random.shuffle).  That way it prevents
>> my local namespace from getting polluted with random symbols imported
>> from modules.
> 
> Or a third way:
> 
> import random
> shuffle = random.shuffle

Our three weapons are:

(1) fully qualified names: 

    import random
    random.shuffle

(2) unqualified local imports: 

    from random import shuffle

(3) local name binding: 

    import random
    shuffle = random.shuffle

(4) messing about under the hood:

    shuffle = __import__('random', fromlist=['shuffle']).shuffle

(5) and a fanatical devotion to the Pope.


A serious question -- what is the point of the fromlist argument to
__import__? It doesn't appear to actually do anything.


https://docs.python.org/3/library/functions.html#__import__



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#77891 — __import__(name, fromlist=...), was Re: Shuffle

FromPeter Otten <__peter__@web.de>
Date2014-09-15 16:30 +0200
Subject__import__(name, fromlist=...), was Re: Shuffle
Message-ID<mailman.14029.1410791470.18130.python-list@python.org>
In reply to#77888
Steven D'Aprano wrote:

> A serious question -- what is the point of the fromlist argument to
> __import__? It doesn't appear to actually do anything.
> 
> 
> https://docs.python.org/3/library/functions.html#__import__

It may be for submodules:

$ mkdir -p aaa/bbb
$ tree
.
└── aaa
    └── bbb

2 directories, 0 files
$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> __import__("aaa").bbb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'bbb'
>>> __import__("aaa", fromlist=["bbb"]).bbb
<module 'aaa.bbb' (namespace)>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web