Last active
December 23, 2017 07:42
-
-
Save safijari/5a1f38bbc8c4d24a8dc889b240dc7e9b to your computer and use it in GitHub Desktop.
python properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PI = 3.141 | |
class Circle: | |
def init(self, radius): | |
self.radius = radius | |
self.area = PI * self.radius ** 2 | |
self.circumference = 2 * PI * self.radius |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageHolder: | |
def __init__(self, image_path): | |
self.image_path = image_path | |
def get_image(self): | |
return load_image_from_path(self.image_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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