Skip to content

Instantly share code, notes, and snippets.

@safijari
Last active December 23, 2017 07:42
Show Gist options
  • Save safijari/5a1f38bbc8c4d24a8dc889b240dc7e9b to your computer and use it in GitHub Desktop.
Save safijari/5a1f38bbc8c4d24a8dc889b240dc7e9b to your computer and use it in GitHub Desktop.
python properties
c = Circle(1)
print c.area # prints 3.141
print c.circumference # prints 6.282
c.radius = 2
print c.area # prints 3.141 which is incorrect
print c.circumference # prints 6.282 which is incorrect
PI = 3.141
class Circle:
def init(self, radius):
self.radius = radius
self.area = PI * self.radius ** 2
self.circumference = 2 * PI * self.radius
class ImageHolder:
def __init__(self, image_path):
self.image_path = image_path
def get_image(self):
return load_image_from_path(self.image_path)
c = Circle(1)
print c.area # prints 3.141
print c.circumference # prints 6.282
c.radius = 2
print c.area # prints 12.564 which is correct!
print c.circumference # prints 12.564 which is correct!
PI = 3.141
class Circle:
def init(self, radius):
self.radius = radius
def area(self):
return PI * self.radius ** 2
def circumference(self):
return 2 * PI * self.radius
# The class that holds onto the image
class ImageHolder:
def __init__(self, image_path):
self.image = load_image_from_path(image_path)
holders = []
# The bit of code that loads the images
for image_path in large_list_of_paths:
holders.append(ImageHolder(image_path))
# Various parts of the pipeline that do something with the images
for holder in holders:
do_something(holder.image)
class ImageHolder:
def __init__(self, image_path):
self.image_path = image_path
@property
def image(self):
return load_image_from_path(self.image_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment