Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. from itertools import *
  2. import random
  3. from sets import Set
  4.  
  5. class Animal(object):
  6.   """docstring for """
  7.   newid = count().next
  8.   def __init__(self):
  9.     self.id = Animal.newid()
  10.  
  11. def print_memory_address(obj):
  12.   for o in obj:
  13.     print "id: %d | Memory address: %d" % (o.id, hash(o))
  14.  
  15. ## Generate 10 Animal objects
  16. animals = [Animal() for i in range(10)]
  17. print_memory_address(animals)
  18.  
  19. ## Shuffle and check the memory address
  20. random.shuffle(animals)
  21. print "Shuffled!"
  22. print_memory_address(animals)
  23.  
  24. ## Check if generating a Set might change the address
  25. immutable = Set([animals[0]])
  26. print "Generated a Set!"
  27. print_memory_address(immutable)
  28.  
  29. ## Check if the memory address might be changed after subtraction
  30. animals = list(Set(animals) - Set([animals[0]]))
  31. print "Removed once!"
  32. print_memory_address(animals)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement