When you were little, your mother used to make the most delicious cookies, which you could not resist. So, every now and then, when your mother didn't see you, you sneaked into the kitchen, climbed onto a stool to reach the cookie jar, and stole a cookie or two. However, sometimes while doing this, you would hear foot steps approaching, so you quickly jumped down from the stool and, when your mother entered the kitchen, you pretended as if nothing had happened (whistle, whistle innocently). However, your mother knew. How did she know? You forgot to put the lid back on the cookie jar! Oh, no!

Growing older (and still not able to resist your mother's cookies), you deviced a contraption that would automatically put the lid back on the cookie jar, _no matter what would happen_.

The class `CookieJar` is provided: 
```Python
class CookieJar(object):

    def __init__(self):
        self._is_open = False

    def take(self):
        if not self._is_open:
            raise ValueError("Cookie jar is closed")
        return "Cookie"

    def open_jar(self):
        self._is_open = True

    def close_jar(self):
        self._is_open = False

    def is_open(self):
        return self._is_open
```
Your task is to implement the 'contraption' `SelfClosing` (class, method, whatever; it's your choice) such that, given an instance`cookie_jar` of `CookieJar`, you may call:

```Python
with SelfClosing(cookie_jar) as jar:
    cookie = jar.take()
```
after which, `cookie_jar.is_open() == False`, _no matter what_.

Do not alter the provided code. (Tests will be performed with a code that differs slightly from the one provided, so modifying it is to no avail.)

Enjoy!

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Starter code: def __init__(self, jar):
	

Solutions:
class SelfClosing(object):

	def __init__(self, closable):
		self.closable = closable

	def __enter__(self):
		self.closable.open_jar()
		return self.closable

	def __exit__(self, *args):
		self.closable.close_jar()


class SelfClosing(object):

	def __init__(self, jar):
		self._jar = jar

	def __enter__(self):
		self._jar.open_jar()
		return self._jar

	def __exit__(self, *args):
		self._jar.close_jar()


class SelfClosing:

	def __init__(self, obj):
		self.object = obj

	def __enter__(self):
		self.object._is_open = True
		return self.object

	def __exit__(self, exc_type, exc_val, exc_tb):
		self.object._is_open = False


class SelfClosing:

	def __init__(self, jar):
		self.jar = jar

	def __enter__(self):
		self.jar._is_open = True
		return self.jar

	def __exit__(self, a, b, c):
		self.jar._is_open = False


class SelfClosing:

	def __init__(self, cookie_jar):
		self.cookie_jar = cookie_jar
		self.cookie_jar._is_open = True

	def __enter__(self):
		return self.cookie_jar

	def __exit__(self, exct, excv, tb):
		self.cookie_jar._is_open = False


class SelfClosing(object):

	def __init__(self, obj):
		self.obj = obj

	def __enter__(self):
		self.obj.open_jar()
		return self.obj

	def __exit__(self, *funcs):
		self.obj.close_jar()


class SelfClosing:

	def __init__(self, obj):
		self.obj = obj

	def __enter__(self):
		self.obj.open_jar()
		return self.obj

	def __exit__(self, *_):
		self.obj.close_jar()


class SelfClosing(object):

	def __init__(self, jar):
		self._jar = jar
		self.take = self._jar.take

	def __enter__(self):
		self._jar.open_jar()
		return self

	def __exit__(self, *a):
		self._jar.close_jar()
SelfClosing = type('SelfClosing', (object,), {'__init__': lambda s, j: (setattr(s, 'j', j), setattr(s, 'take', s.j.take))[0], '__enter__': lambda s: (s.j.open_jar(), s)[1], '__exit__': lambda s, *a: s.j.close_jar()})