Python Ellipsis

I ran across this oddity experimenting with getting the locals() to print from a for loop.

a stack overflow reference I used for help:
https://stackoverflow.com/questions/26544091/checking-if-type-list-in-python

I don't know if I'll get through it tonight to explain, or figure it out, maybe tomorrow.
But, check it out.
Curious.
Lets break it!

Update 12/22/2020:
The three dots [...] Ellipsis, are a python placeholder for a big list of items... basically. 
Here's A stackoverflow link with more eloquent answers:

UPDATE 10/9/18 below




I asked about this in the LearnCodeTheHardWay forum to see if I could get suggestions how to decipher this.
On suggestion, I ran some profile tests on two separate codes.
What I really want to know is WTH is that [...] called, and what is python doing there?

Picture & Code for test on the size 1 million for loop locals() list:
** 1.993 seconds ***

















Code:

#!usr/bin/python3
import cProfile


# how it should be done:
# all_locals = locals()

# def find_locals():
    # alist = []
    # for item in all_locals:
        #alist.append(item)
    # return alist
"""
When I ran this in my shell, the list looked infinite, but it would have
crashed if the list did not end.
So what is the limit on for loops?
"""
def get_locals():
    alist = []
    for item in locals().items():
        alist.append(item)
    return alist

def test_for_loop():
    x = get_locals()
    print(x)
    print(len(x))
    fragments = x[0][1]
    print('type of fragments =', type(fragments))
    manylists = []
    while isinstance(fragments, list):
        manylists.append(fragments)
        fragments = fragments[0][1]
     #print('type of fragments after first while loop=', type(fragments))
        if len(manylists) > 1000000:
            break
    print("length =",len(manylists))
    for i in range(0, 10):
        print(manylists[i])   

if __name__ == '__main__':
    test_for_loop()
   
#####################################
   Second test, just a for loop making a list:    **   1.862 seconds
####################################

















### code ###
#!usr/bin/python3
import cProfile

"""

test a for loop that makes a list of one million

"""

def test_one_mil_for_loop():
    alist = []
    #string = 'loop'
    for x in range(0, 1000000):
        string = 'loop' + str(x)
        alist.append('string')
    return alist

if __name__ == '__main__':
    test_one_mil_for_loop()

Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame