-1

I have some streets names and I've made a function to read those names. I need to add these streets to a list, but the problem is I override the list everytime the function is accessed. I want to keep it without writting it to an external file.

import random 

def readAddress(self,streetName):
        self.streetName   = streetName
           
        #declare list, it overrides the list with an empty one
        addressList = []
        
        if streetName is not in addressLis:         
           addressList.append(streetName)               
            
        newName   =  rd.choice(addressList)  

I've also tried an extra function to maintain the values

 def keepList(self,extList):        
        self.extList = extList        
           
        return extList

And changed readAddress to use it. But of course I am replacing the list and not extending it. I also tried to yield the names in the external function. None worked.


def readAddress(self,streetName):
        self.streetName   = streetName
           
        #declare list, it overrides the list with an empty one
        addressList = []
        
        if streetName is not in addressLis:         
          addressList.append(streetName)  
        
        adress=keepList(addressList)  
                     
        newName   =  rd.choice(address) 

The input data is:

address1 = modifyAddress('Naciones')
address2 = modifyAddress('Juncal')
address3 = modifyAddress('Rocafort')
address4 = modifyAddress('Mare de Déu de Bellvitge')

The print of address should be:

['Naciones','Juncal','Rocafort','Mare de Déu de Bellvitge]

powerPixie
  • 718
  • 9
  • 20
  • 2
    Does this answer your question? [List changes unexpectedly after assignment. How do I clone or copy it to prevent this?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-how-do-i-clone-or-copy-it-to-prevent) – MoonMist Jul 14 '20 at 11:05
  • Can you please clarify what you are trying to do? When setting ``addressList = []``, ``addressList`` is not defined beforehand so there is nothing to be overwritten. For some reason, all your *functions* take a ``self`` parameter – are they actually methods? Why don't you store ``addressList`` on ``self``? Also note that the code is not functional – ``self`` is present but no ``class``, ``keepList`` is called without ``self``, ``random`` is imported but used as ``rd``, and the final code block uses ``modifyAddress`` which is not defined anywhere. – MisterMiyagi Jul 14 '20 at 11:38
  • I extracted from my code, which is inside a class. I tried to expose the problems – powerPixie Jul 14 '20 at 11:45
  • If this is inside a class, then why is ``addressList`` not stored on the instance? Note that using a class is likely the most appropriate solution, so if there already is one please include it in the question. See the [mcve] page for details, but note that code which is *both* minimal *as well as complete* (to reproduce the given issue) is ideal. – MisterMiyagi Jul 14 '20 at 11:51

1 Answers1

1

Declare it as global.

import random 
addressList = []
def readAddress(self,streetName):
        self.streetName   = streetName
           
        #declare list, it overrides the list with an empty one
        global addressList
        
        if streetName is not in addressList:         
           addressList.append(streetName)               
            
        newName   =  rd.choice(addressList)  

Additionally, you have a typo at if streetName is not in addressLis: It should be if streetName is not in addressList:.

Placing the addressList as a class variable is the best way :

import random
def Address:
    def __init__(self):
        self.addressList = []
    def readAddress(self,streetName):                  
        if streetName is not in self.addressList:         
           self.addressList.append(streetName)                           
        newName   =  rd.choice(addressList)  
        return newName

Initializing :

Addr = Address()
Addr.readAddress(streetName)       #Function call.
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40