[Python-de] Re: Funktion ueberschreiben?

Mike Müller <mmueller-Nw5JoyH4ZXncp1/[email protected]>
Newsgroups gmane.comp.python.general.german
Organization Python Academy GmbH & Co. KG
Message-ID <[email protected]>
Am 03.12.22 um 10:53 schrieb Stefan Ram:
> =?UTF-8?Q?Mike_M=c3=bcller?= <[email protected]> writes:
>> import duplicity.dup_time
>> def timetopretty(timeinseconds):
>>      """Return pretty version of time"""
>>      return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(timeinseconds))
>> duplicity.dup_time.timetopretty = timetopretty
>> ganz zu Beginn des Programms sollte funktionieren. Wichtig ist, dass
>> duplicity.dup_time nicht schon vorher importiert wird. Sonst müsstest du es
>> vorher aus sys.modules entfernen.
> 
>    Diese Einschränkung verstehe ich nicht ganz. Ich dachte, daß sich
>    das "monkey patching" ab dem Zeitpunkt seiner Ausführung auswirkt.
>    In Deinem Beispiel wird doch auch "schon vorher importiert", also
>    als erstes importiert, dann gepatcht, und ab da wirkt es. Vergleiche:

Du hast Recht. Diese Einschränkung bezieht sich nur auf das Ändern des
Quelltext in einem Modul und einen folgenden Neu-Import der Datei im laufenden
Prozess. Da du dem Namen eine neue Funktion zuweist, müsste es ab diesem
Zeitpunkt überall im Programm wirksam sein. Das mit dem Löschen aus
sys.modules ist nur beim Entwickeln z.B. in Notebooks wichtig, wenn man den
Code in einem Modul ändert und dann neu importiert ohne das Notebook neu zu
starten.

> import tkinter.messagebox
> tkinter.messagebox.showinfo( "arg", "arg1" )
> tkinter.messagebox.showinfo = print
> tkinter.messagebox.showinfo( "arg", "arg1"  )
> 
>    .
> 
> _______________________________________________
> python-de Mailingliste -- [email protected]
> Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected]
> https://mail.python.org/mailman3/lists/python-de.python.org/
> Mitgliedsadresse: [email protected]

_______________________________________________
python-de Mailingliste -- [email protected]
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected]
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.