Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.python Subject: Re: Is Lexical Binding The Norm Yet? Date: 21 Jan 2024 07:28:12 GMT Organization: Stefan Ram Lines: 64 Expires: 1 Dec 2024 11:59:58 GMT Message-ID: References: <86r0ijsdyj.fsf@williamsburg.bawden.org> <871qajgr9k.fsf@nightsong.com> <20240117122629.806@kylheku.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de yZUfescOwqQ/D+VmzaFfnweZciYqIHmPV9HHyzbWk3n5T3 Cancel-Lock: sha1:pqlF2reCMa4v18Q7Ua7YSPUUWM0= sha256:Y2mdvl9ISDPk8hEF4G+W2lfBlmAA34KnCR5FMJZ6AoA= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Accept-Language: de-DE-1901, en-US, it, fr-FR Newsgroups: comp.lang.python ram@zedat.fu-berlin.de (Stefan Ram) writes: >from types import * What do you mean, the cloning part is missing? import types # create a new object def counter_object(): pass # attach a numeric field to the object counter_object.counter_value = 0 # define a named function def increment_value( self ): self.counter_value += 1 # attach the named function to the object as a method counter_object.increment_value = \ types.MethodType( increment_value, counter_object ) # call the method counter_object.increment_value() # attach an unnamed function to the object counter_object.get_value = \ types.MethodType( lambda self: self.counter_value, counter_object ) # call that method # Prints "1". print( counter_object.get_value() ) # An ad-hoc function to clone an object for the purpose of "prototype # inheritance". Might need to be refined for more general uses. def clone_object( object_ ): def new_object(): pass for attribute in dir( object_ ): if attribute[ :2 ]!= '__': if type( getattr( object_, attribute ))== types.MethodType: method = getattr( object_, attribute ) function = types.FunctionType( method.__code__, {} ) method = types.MethodType( function, new_object ) setattr( new_object, attribute, method ) else: field = getattr( object_, attribute ) setattr( new_object, attribute, field ) return new_object # create two "clones" ("clone objects") clone = clone_object( counter_object ) other = clone_object( counter_object ) # The clones "inherited" the counter field including its value. # Prints "1 1 1". print( counter_object.get_value(), clone.get_value(), other.get_value() ) # Each object's counter has a value independent from the other object's # counter. other.increment_value() # Prints "1 1 2" print( counter_object.get_value(), clone.get_value(), other.get_value() )