zoukankan      html  css  js  c++  java
  • Python:Opening Python Classes

    I won’t reply to that post much, because it’s mostly… well, not useful to respond to. But people often talk about the wonders of Open Classes in Ruby. For Python people who aren’t familiar with what that means, you can do:

    1 # Somehow acquire SomeClassThatAlreadyExists
    2 class SomeClassThatAlreadyExists
    3     def some_method(blahblahblah)
    4         stuff
    5     end
    6 end

    And SomeClassThatAlreadyExists has a some_method added to it (or if that method already exists, then the method is replaced with the new implementation).

    In Python when you do this, you’ve defined an entirely new class that just happens to have the nameSomeClassThatAlreadyExists. It doesn’t actually effect the original class, and probably will leave you confused because of the two very different classes with the same name. In Ruby when you define a class that already exists, you are extending the class in-place.

    You can change Python classes in-place, but there’s no special syntax for it, so people either think you can’t do it, or don’t realize that you are doing the same thing as in Ruby but without the syntactic help. I guess this will be easier with class decorators, but some time ago I also wrote a recipe using normal decorators that looks like this:

    1 @magic_set(SomeClassThatAlreadyExists)
    2 def some_method(self, blahblahblah):
    3     stuff

    The only thing that is even slightly magic about the setting is that I look at the first argument of the function to determine if you are adding an instance, class, or static method to an object, and let you add it to classes or instances. It’s really not that magic, even if it is called magicset.

    I think with class decorators you could do this:

    1 @extend(SomeClassThatAlreadyExists)
    2 class SomeClassThatAlreadyExists:
    3     def some_method(self, blahblahblah):
    4         stuff

    Implemented like this:

    1 def extend(class_to_extend):
    2     def decorator(extending_class):
    3         class_to_extend.__dict__.update(extending_class.__dict__)
    4         return class_to_extend
    5     return decorator
    
    
  • 相关阅读:
    【新提醒】N820 N821 android 4.2 V1.1版
    Silk Icons —— 再来 700 个免费小图标
    解决基于BAE python+bottle开发上的一系列问题
    gae 已死,别整什么反向代理了,用sae吧,还有在线编辑工具
    MDCC为移动开发者服务:一看、一聊、一聚
    Testin_百度百科
    我们的开源项目-暨开源力量公开课年度庆典活动
    Kivy: Crossplatform Framework for NUI
    进京通行证_百度百科
    史丹·温斯坦称傲牛熊市的秘密_百度百科
  • 原文地址:https://www.cnblogs.com/happyframework/p/3636541.html
Copyright © 2011-2022 走看看