Jaap Woldringh <jjhwoldri...@ziggo.nl> added the comment:

Op 17-01-2020 om 14:03 schreef Karthikeyan Singaravelan:
> Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:
>
> You're referencing to the same list 3 times in B. So modifying it once means 
> all the elements referring to same object reflect the change. Make a copy of 
> the list during append to ensure modification of one doesn't affect other. 
> This is not a python bug.
>
> ----------
> nosy: +xtreak
>
> _______________________________________
> Python tracker <rep...@bugs.python.org>
> <https://bugs.python.org/issue39368>
> _______________________________________

Ah, I see! Thank you for pointing this out to me! I have been pondering 
this for days on end on what was happening here.

This behaviour of Python is very frustrating, and even if "it is not a 
bug" (I knew it!) it's very nasty, and shouldn't be. Your suggestion of 
making a copy of the list during the append saves my day! And Python itself!

I was creating a very simple program for encrypting a sentence using a 
square matrix, and expected a one hour job at most. It became days, and 
I finally created a very simple method, without a rxk matrix itself, but 
using the idea!

So, if possible, please, improve Python so that this feature doesn't 
cause these unnecessary problems. Meanwhile I retract my bug report!

To my opinion this feature of Python, having aparently same objects 
refer to the same addresses in memory, was useful in times long ago when 
memory was expensive. But now this feature is not feature at all. To my 
opinion :)

Gtsori menf egr

Jaap Woldringh

----------
Added file: https://bugs.python.org/file48850/code.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39368>
_______________________________________
### een nieuwe wijze van coderen met behulp
### van een denkbeeldige vierkante matrix
###
def codeertekst(tekst):
    lengte = len(tekst)
    # bepalen dimensie vierkante matrix
    j=1
    while j*j<lengte:
        j+=1
    # lengte tekst aanvullen tot j*j matrix
    # met spaties.
    tekst+=(j*j-lengte)*" "
    # nieuwe even lange tekst aanmaken
    codetekst=tekst

    # lists maken van beide teksten
    # omdat strings immutable zijn en lists niet.
    tekst=list(tekst)
    codetekst=list(codetekst)
    
    # de eigenlijke codering: in de denkbeeldige
    # vierkante matrix rijen en kolommen verwisselen
    # je kunt dit het beste zien door twee j x j matrices
    # te tekenen en in te vullen op papier met korte tekst
    for r in range(j):
        for k in range(j):
            codetekst[r*j+k] = tekst[r+k*j]     ## THIS IS WHERE THE MAGIC IS!

    # conversie lijst van karakters codetekst naar
    # de string code.       
    code=""
    for i in range(j*j):
        code += codetekst[i]
                
    return code         # de gecodeerde tekst
    
def invoer():
    tekst = input("Tekst, afsluiten met enter : ")
    
    return tekst        # de ingevoerde tekst
    
###
# programma dat dit gebruikt.
###
tekst = invoer()
code = codeertekst(tekst)
print(tekst)
print()
print (code)
print()

'''
Gebruik: (kan dus heen en terug, op dezelfde manier!)

jaap@bromo:~$ cd Bureaublad
jaap@bromo:~/Bureaublad$ python3 code.py 
Tekst, afsluiten met enter : Korte tekst.
Korte tekst.

Kek o s rtt te. 

jaap@bromo:~/Bureaublad$ python3 code.py 
Tekst, afsluiten met enter : Kek o s rtt te.
Kek o s rtt te.

Korte tekst.    

'''


_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to