##import ABstract class and abstractmethods for defining interfaces from abc import ABCMeta, abstractmethod ### creating an virtual class with no constructor but with a metaclass and define all methods which are valid in this ABC class Shape(object): __metaclass__ = ABCMeta @abstractmethod def draw(self): print("Shape printed with n number of points, not specified how much") def erase(self): print("Shape erased") ##creating an concrete class based on ABC Shape class Circle(Shape): def draw(self): print("Circle printed! with endless points") def erase(self): print("Circle will not be erased. It always exist") class Square(Shape): def draw(self): print("Square printed! with four points") class Triangle(Shape): def draw(self): print("Triangle printed! with three points") ###create losts of differents shapes and call draw which is implemented in different ways def tryPoly(): ##set up a list of shapes listshapes = [Circle(), Circle(), Square(), Square(), Circle(), Square(), Triangle(), Triangle()] for each in listshapes: each.draw() each.erase() tryPoly()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question