{"action":"created","issue":{"url":"https://api.github.com/repos/sergiotrujillomtz/python-poo/issues/1","repository_url":"https://api.github.com/repos/sergiotrujillomtz/python-poo","labels_url":"https://api.github.com/repos/sergiotrujillomtz/python-poo/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/sergiotrujillomtz/python-poo/issues/1/comments","events_url":"https://api.github.com/repos/sergiotrujillomtz/python-poo/issues/1/events","html_url":"https://github.com/sergiotrujillomtz/python-poo/issues/1","id":131709413,"number":1,"title":"How to do heritage in python?","user":{"login":"tonilopezmr","id":5845622,"avatar_url":"https://avatars.githubusercontent.com/u/5845622?v=3","gravatar_id":"","url":"https://api.github.com/users/tonilopezmr","html_url":"https://github.com/tonilopezmr","followers_url":"https://api.github.com/users/tonilopezmr/followers","following_url":"https://api.github.com/users/tonilopezmr/following{/other_user}","gists_url":"https://api.github.com/users/tonilopezmr/gists{/gist_id}","starred_url":"https://api.github.com/users/tonilopezmr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tonilopezmr/subscriptions","organizations_url":"https://api.github.com/users/tonilopezmr/orgs","repos_url":"https://api.github.com/users/tonilopezmr/repos","events_url":"https://api.github.com/users/tonilopezmr/events{/privacy}","received_events_url":"https://api.github.com/users/tonilopezmr/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-02-05T17:52:32Z","updated_at":"2016-02-10T22:40:23Z","closed_at":null,"body":"Could you show me a simple heritage code in python?\r\n\r\nThanks you!!"},"comment":{"url":"https://api.github.com/repos/sergiotrujillomtz/python-poo/issues/comments/182614564","html_url":"https://github.com/sergiotrujillomtz/python-poo/issues/1#issuecomment-182614564","issue_url":"https://api.github.com/repos/sergiotrujillomtz/python-poo/issues/1","id":182614564,"user":{"login":"sergiotrujillomtz","id":15813309,"avatar_url":"https://avatars.githubusercontent.com/u/15813309?v=3","gravatar_id":"","url":"https://api.github.com/users/sergiotrujillomtz","html_url":"https://github.com/sergiotrujillomtz","followers_url":"https://api.github.com/users/sergiotrujillomtz/followers","following_url":"https://api.github.com/users/sergiotrujillomtz/following{/other_user}","gists_url":"https://api.github.com/users/sergiotrujillomtz/gists{/gist_id}","starred_url":"https://api.github.com/users/sergiotrujillomtz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sergiotrujillomtz/subscriptions","organizations_url":"https://api.github.com/users/sergiotrujillomtz/orgs","repos_url":"https://api.github.com/users/sergiotrujillomtz/repos","events_url":"https://api.github.com/users/sergiotrujillomtz/events{/privacy}","received_events_url":"https://api.github.com/users/sergiotrujillomtz/received_events","type":"User","site_admin":false},"created_at":"2016-02-10T22:40:23Z","updated_at":"2016-02-10T22:40:23Z","body":"Hello @tonilopezmr, first I'll give you a brief explanation of inheritance in Python, doing a little comparison with the Java language.\r\nAs in the Java language, all objects inherit from the Object class. However there are some minor differences. In the Python language there is multiple inheritance and interfaces not exists as such. To achieve the same effect as Java interfaces, you can create classes that inherit from other abstract classes. You can found more information in [https://docs.python.org/2.7/tutorial/classes.html#inheritance](https://docs.python.org/2.7/tutorial/classes.html#inheritance)\r\nThis is a sample for simple inheritance:\r\n\r\n    class InterfaceClass(object):\r\n\r\n        def do_something_1(self):\r\n            raise NotImplementedError(\"Should have implemented this\")\r\n\r\n        def do_something_2(self):\r\n            raise NotImplementedError(\"Should have implemented this\")\r\n\r\n\r\n    class AbstractClass(InterfaceClass):\r\n\r\n        def do_something_1(self):\r\n            \"\"\"\r\n                Implementation\r\n            \"\"\"\r\n            return 1\r\n\r\n        def do_something_2(self):\r\n            \"\"\"\r\n                Implementation\r\n            \"\"\"\r\n            return 2\r\n\r\n        def do_something_3(self):\r\n            raise NotImplementedError(\"Should have implemented this\")\r\n\r\n\r\n    class ConcreteClass(AbstractClass):\r\n\r\n        def do_something_3(self):\r\n            \"\"\"\r\n                Implementation\r\n            \"\"\"\r\n            return 3\r\n\r\nIn response to your question,\r\n> How can I do a utility class in python?\r\n\r\nwhat is usually done in Python is to create a module, which is not a class, it is a simple Python file containing all utility functions. Then you can import the module into any other implementation and use these functions. For example:\r\nI have the package `utility_module` with 2 files; `math_functions.py` and `__init__.py`:\r\n\r\n`__init__.py`:\r\n\r\n    from . import math_functions\r\n\r\n`math_functions.py`:\r\n\r\n    def factorial(n):\r\n        if not isinstance(n, int) or n < 0:\r\n            raise ValueError(\"n is not integral or is negative\")\r\n        return n == 0 and 1 or n * factorial(n - 1)\r\n\r\n    def fibonacci(n):\r\n        if not isinstance(n, int) or n < 0:\r\n            raise ValueError(\"n is not integral or is negative\")\r\n        return 0 if n == 0 else 1 if n == 1 else fibonacci(n-1) + fibonacci(n-2)\r\n\r\nAny implementation can import the module and use its functions. For example:\r\n\r\n    from utility_module import math_functions\r\n    from utility_module.math_functions import fibonacci\r\n\r\n    if __name__ == '__main__':\r\n        print \"7! = \", math_functions.factorial(7)\r\n        print \"fibonacci(8) = \", fibonacci(8)"}}