[python] změna hodnoty v modulu v inicializaci def __init__ (self)

Petr Messner petr.messner na gmail.com
Středa Březen 25 17:24:11 CET 2015


Šlo by, když už to teda musí být...

Za předpokladu, že v modul.py je tohle:

class Trida:
    def __init__(self):
        self.var = 100

    def program(self):
        print self.var

Pak to lze udělat takto, je to spíš hack:

import modul

t = modul.Trida()
t.program() # => 100

StaraTrida = modul.Trida

class NovaTrida (StaraTrida):

    def __init__(self):
        StaraTrida.__init__(self)
        self.var = 50

modul.Trida = NovaTrida

t = modul.Trida()
t.program() # => 50

Je to to, co potřebujete?


Dne 25. března 2015 17:13 "Ing. Vladislav Ludík" <vlada na ludik.cz> napsal(a):

> Bez úpravy modulu to asi nejde, že ?
>
> Vlada
>
> Dne 25.3.2015 v 17:06 starenka . napsal(a):
>
>  class Trida:
>>      def __init__(self, var=100):
>>          self.var = var
>>
>>      def program(self):
>>          print self.var
>>
>> T = Trida(var=500)
>> T.program()
>>
>>> 500
>>>
>> ---
>> In Perl you shoot yourself in the foot, but nobody can understand how
>> you did it. Six months later, neither can you. | print
>> 'aknerats'[::-1]
>>
>>
>> 2015-03-25 16:29 GMT+01:00 "Ing. Vladislav Ludík" <vlada na ludik.cz>:
>>
>>> Zdravím všechny,
>>>
>>> prosím o radu, jak změnit hodnotu v modulu:
>>>
>>> class Trida:
>>>      def __init__(self):
>>>          self.var = 100
>>>
>>>      def program(self):
>>>          print self.var
>>>
>>> Naimportuju modul a zavolám:
>>>
>>> T = Trida()
>>> T.program()
>>>
>>> vytiskne 100
>>>
>>> Jak nastavím hodnotu self.var ?
>>>
>>> Děkuji Vlada
>>> _______________________________________________
>>> Python mailing list
>>> python na py.cz
>>> http://www.py.cz/mailman/listinfo/python
>>>
>>> Visit: http://www.py.cz
>>>
>> _______________________________________________
>> Python mailing list
>> python na py.cz
>> http://www.py.cz/mailman/listinfo/python
>>
>> Visit: http://www.py.cz
>>
>
> _______________________________________________
> Python mailing list
> python na py.cz
> http://www.py.cz/mailman/listinfo/python
>
> Visit: http://www.py.cz
>
------------- další část ---------------
HTML příloha byla odstraněna...
URL: <http://www.py.cz/pipermail/python/attachments/20150325/acfcdf72/attachment.html>


Další informace o konferenci Python