Retourner au contenu associé (dépêche : Numba 0.14)
Posté par THE_ALF_ le 19 septembre 2014 à 15:29. En réponse à la dépêche Numba 0.14. Évalué à 2.
Facile !
In [1]: class Typed(object): ...: def __init__(self, t): ...: self.type = t ...: def __setattr__(self, name, value): ...: if hasattr(self,"type"): ...: self.__dict__[name] = self.type(value) ...: else: ...: self.__dict__["type"] = value ...: In [2]: Int = Typed(int) In [3]: Int.a = 1.23 In [4]: Int.a Out[4]: 1 In [5]: Int.a = "toto" --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-21393c614655> in <module>() ----> 1 Int.a = "toto" <ipython-input-1-e04bf26fc6b5> in __setattr__(self, name, value) 4 def __setattr__(self, name, value): 5 if hasattr(self,"type"): ----> 6 self.__dict__[name] = self.type(value) 7 else: 8 self.__dict__["type"] = value ValueError: invalid literal for int() with base 10: 'toto' In [6]: Float = Typed(float) In [7]: Float.toto = 1 In [8]: Float.toto Out[8]: 1.0 In [9]: Str = Typed(str) In [10]: Str.truc = 1 In [11]: Str.truc Out[11]: '1'
Implémentation minimale, on peut raffiner, mais c'est l'idée ;-)
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
[^] # Re: Retour sur cpython ?
Posté par THE_ALF_ . En réponse à la dépêche Numba 0.14. Évalué à 2.
Facile !
Implémentation minimale, on peut raffiner, mais c'est l'idée ;-)