AP. Type/Identity/Value

Run Settings
LanguagePython
Language Version
Run Command
def printOut(x, spacing=20): args = { "value": x, "type": type(x), "identity": id(x), "spacing": spacing, "fill": "-", } print """{type:<{spacing}} - type of variable {identity:<{spacing}} - identity of variable {value!r:<{spacing}} - value of variable {fill:{fill}<{spacing}}""".format(**args) def compare(remote, local): print "Remove value:" printOut(remote) print "\nLocal value:" printOut(local) print remote == local, ":local == remote" print id(remote) == id(local), ": id(local) == id(remote)" print "{:+^100}\n".format("") # Any object created in python has its type, identity and value. # Here's an example of how all of these parameters are looks like. x = "new string object" printOut(x) # Since type and indentity does not change during object live-time, so we can # devide objects on two categories: print "\n{:=^100}\n".format("Mutable") ## Whose values can change - Mutable. List and dict are among them. # Let's excersise and check does identity changes if list has been changed? lst = [1] printOut(lst) # Now change `lst` and take a look on what really happens? lst.append(lst) print "\nList has been changed:" printOut(lst) # Everything is as we stated. Identity is the same, type is the same, value is # changed. print "\n{:=^100}\n".format("Immutable") ## Whose values cann't be changed - Immutable. Numbers, strings, sets, tuples are # among them. Let's check does two immutable created differently but with same # value has same identity? print "{:-^100}\n".format("Remote values") # Here's in file 'string_value' I saved a string to be sure that values are # independent. In file and locally we have the same string 'Herisson'. remote_string_value = open('string_value').read() local_string_value = "Herisson" compare(remote_string_value, local_string_value) # Here's an example for integer remote_integer_value = int(open('integer_value').read()) local_integer_value = 12345678 compare(remote_integer_value, local_integer_value) print "{:-^100}\n".format("Other module values") # But what about values came from other module? import tessera compare(tessera.string_value, local_string_value) compare(tessera.integer_value, local_integer_value) print "{:-^100}\n".format("Internal module values") # But what about values in same module? class Inner: def __init__(self): self.string_value = 'Herisson' self.integer_value = 12345678 inner = Inner() compare(inner.string_value, local_string_value) compare(inner.integer_value, local_integer_value) # So lets make a conclusion: print """Identities of objects with same values could not be the same."""
Herisson
12345678
string_value = 'Herisson' integer_value = 12345678
Editor Settings
Theme
Key bindings
Full width
Lines