The Art of PyGramming

Writing code is an art, just like painting or writing poetry. When you see a beautiful painting, you appreciate the artist. You do not give credit to the paints, or canvas used to create it. Likewise, when you see a well-written piece of software, the merit belongs to the developer; not to the programming language or the text editor that he used to write it.

OOPs — Python did it again!

OOPs! This is a tough nut to crack
class HazelNut:
"""
My favorite nut.
"""
def __init__(
self, size, shape):
"""
Creates a hazelnut.
:param int size
Size of nut.
:param str shape
Shape of nut.
"""
self.size = size
self.shape = shape
my_nut = HazelNut('bigggg-nut', 'round')
my_nut.owner = "Dale"
my_nut.shape = "chip's tail"
from enum import Enum as _Enumclass HazelNut:
"""
My favorite nut.
"""
__slots__ = ['__size', '__shape'] def __init__(self, size, shape):
"""
Creates a hazelnut.
:param int size
Size of nut.
:param str shape
Shape of nut.
"""
self.__size = _verify_nut_size(size)
self.__shape = HazelNutShape(shape)
@property
def size(self):
"""
Gets the size of the nut.
:rtype: int
"""
return self.__size

@property
def shape(self):
"""
Gets the shape of the nut.
:rtype: HazelNutShape
"""
return self.__shape
def _verify_nut_size(size):
if not isinstance(size, int):
raise TypeError('size should be an integer')
if size <= 0:
raise ValueError('size should be greater than 0')
return size
class HazelNutShape(_Enum):
"""
Encapsulates the shapes of a hazelnut
"""
ROUND = 'round'
HALF_MOON = 'half-moon'

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store