Ahoj,
jj, jde.
T = Trida()
T.var = 50
T.program()
Instancni promenna v pythonu neni privatni, muzes do ni volne zapisovat.
Pouze pokud nazev promenne zacina na podtrzitko, tak Ti tim vyvojar
rika, ze to je neco interniho a nemel bys s tim pracovat naprimo.
Dan
Dne 25.3.2015 v 17:13 Ing.
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
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
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
T = Trida(var=500) nefunguje.
Hlásí:
TypeError: __init__() got an unexpected keyword argument 'var'
Ale zjistil jsem, že funguje jednoduché:
T.var = 500
Vlada
Dne 25.3.2015 v 17:17 Daniel Milde napsal(a):
Ahoj,
jj, jde.
T = Trida()
T.var = 50
T.program()
Instancni promenna v pythonu neni
Š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 =