python - Best way to implement/call a class that returns an immutable value? -


i in python:

result = someclass(some_argument) 

here catch though. don't want result instance immutable object (int, example). hole role of class returning value calculated argument. using class , not function dry purposes. since above code won't work because return instance of someclass best alternative?

my idea have static method, don't it:

result = someclass.static_method(some_argument) 

you can override __new__. this idea and/or necessary though ...

>>> class foo(object): ...     def __new__(cls): ...         return 1 ...  >>> foo() 1 >>> type(foo()) <type 'int'> 

if don't return instance of cls, __init__ never called.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -