Languages/Python2014.12.05 14:09


import six

import abc


@six.add_metaclass(abc.ABCMeta)

class MyAbstractClass(object):


    @abc.abstractmethod

    def abstract_method(self):

        while False:

            yield None


    def public_method(self):

        self.abstract_method()


class MyConcreteClass(MyAbstractClass):


    def abstract_method(self):

        '''

        if this method is omitted, following error is raised

        when public_method is called against an object.

        

            TypeError: Can't instantiate abstract class MyConcreteClass

            with abstract methods abstract_method


        '''

        print "abstract method is implemented."


    def __init__(self):

        pass


#

# below shows list of all the abstract methods.

#

print MyAbstractClass.__abstractmethods__

print MyAbstractClass.__mro__ # show method resolution order

print MyConcreteClass.__abstractmethods__

print MyConcreteClass.__mro__ # show method resolution order


#

# create an object of MyConcreteClass

# and call its 'public_method'.

#

entity = MyConcreteClass()

entity.public_method()


저작자 표시 비영리 변경 금지
신고
Posted by 이병준

소중한 의견, 감사합니다. ^^