python - How to return a value from a method back to the input value -


i have created helper method check zip code format country. because have multiple zip codes (e.g. visit, postal) like use helper method. when debug can see self.zip put value zipcode, when runs trough method zipcode updates should, not return value self.zip.

could explain me how work?

def onchange_zip(self):     self.postal_code_format(self.zip, self.country_id)  def postal_code_format(self, zipcode, country):     if country.name == "netherlands":         zipcode = zipcode.replace(" ", "").upper()         if len(zipcode) == 6:             numbers = zipcode[:4]             letters = zipcode[-2:]             if letters.isalpha() , numbers.isdigit():                 zipcode = str("{0} {1}").format(numbers, letters)             else:                 raise valueerror("could not format postal code.")         else:             raise valueerror("could not format postal code.")         return zipcode 

when say

zipcode = zipcode.replace(" ", "").upper() 

you making zipcode refer new string object. no longer refers self.zip object.

the right way assigning value self.zip this

self.zip = self.postal_code_format(self.zip, self.country_id) 

or reassign value in postal_code_format function itself, instead of returning, this

self.zip = zipcode 

note: string objects immutable anyway. means that, operations on string objects give new string object, not modify original object. example,

>>> string_obj = 'thefourtheye' >>> string_obj.upper() 'thefourtheye' >>> string_obj 'thefourtheye' 

as see here, string_obj.upper() returns new string object upper case letters, original object remains unchanged. cannot change value @ self.zip.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -