美文网首页
类与实例

类与实例

作者: Rim99 | 来源:发表于2015-12-14 16:08 被阅读19次

    定义实例

    一个实例本质上是一个字典,里面包含的是:“方法名”-“函数”对

    def make_instance(cls):
            """Return a new object instance, which is a dispatch dictionary."""
            def get_value(name):
                if name in attributes:
                    return attributes[name]
                else:
                    value = cls['get'](name)
                    return bind_method(value, instance)
            def set_value(name, value):
                attributes[name] = value
            attributes = {}
            instance = {'get': get_value, 'set': set_value}
            return instance
            
    def bind_method(value, instance):
            """Return a bound method if value is callable, or value otherwise."""
            if callable(value):
                def method(*args):
                    return value(instance, *args)
                return method
            else:
                return value
    

    定义类

    def make_class(attributes, base_class=None):
            """Return a new class, which is a dispatch dictionary."""
            def get_value(name):
                if name in attributes:
                    return attributes[name]
                elif base_class is not None:
                    return base_class['get'](name)
            def set_value(name, value):
                attributes[name] = value
            def new(*args):
                return init_instance(cls, *args)
            cls = {'get': get_value, 'set': set_value, 'new': new}
            return cls
            
            
    def init_instance(cls, *args):
            """Return a new object with type cls, initialized with args."""
            instance = make_instance(cls)
            init = cls['get']('__init__')
            if init:
                init(instance, *args)
            return instance
    

    栗子

    def make_account_class():
            """Return the Account class, which has deposit and withdraw methods."""
            interest = 0.02
            def __init__(self, account_holder):
                self['set']('holder', account_holder)
                self['set']('balance', 0)
            def deposit(self, amount):
                """Increase the account balance by amount and return the new balance."""
                new_balance = self['get']('balance') + amount
                self['set']('balance', new_balance)
                return self['get']('balance')
            def withdraw(self, amount):
                """Decrease the account balance by amount and return the new balance."""
                balance = self['get']('balance')
                if amount > balance:
                    return 'Insufficient funds'
                self['set']('balance', balance - amount)
                return self['get']('balance')
            return make_class(locals()) 
    

    最后一句locals()返回一个当前框架下的字典。

    Account类可使用下句生成:

    >>> Account = make_account_class()
    

    子类关于继承的实现:

    def make_checking_account_class():
            """Return the CheckingAccount class, which imposes a $1 withdrawal fee."""
            interest = 0.01
            withdraw_fee = 1
            def withdraw(self, amount):
                fee = self['get']('withdraw_fee')
                return Account['get']('withdraw')(self, amount + fee)
            return make_class(locals(), Account)
    
    
    >>> CheckingAccount = make_checking_account_class() #子类的生成
    >>> jack_acct = CheckingAccount['new']('Spock') #子类实例的生成
    >>> jack_acct['get']('interest') #子类实例的方法调用
    0.01
    

    相关文章

      网友评论

          本文标题:类与实例

          本文链接:https://www.haomeiwen.com/subject/ypduhttx.html