Programming language for self-modifying code?

后端 未结 14 1791
别那么骄傲
别那么骄傲 2020-12-23 13:05
  • I am recently thinking about writing self-modifying programs, I think it may be powerful and fun. So I am currently looking for a language that allows
14条回答
  •  爱一瞬间的悲伤
    2020-12-23 13:48

    May I suggest Python, a nice very high-level dynamic language which has rich introspection included (and by e.g. usage of compile, eval or exec permits a form of self-modifying code). A very simple example based upon your question:

    def label1(a,b,c):
        c=a+b
        return c
    
    a,b,c=10,20,0    
    print label1(a,b,c) # prints 30
    
    newdef= \
    """
    def label1(a,b,c):
        c=a*b
        return c
    """
    exec(newdef,globals(),globals())
    
    print label1(a,b,c) # prints 200
    

    Note that in the code sample above c is only altered in the function scope.

提交回复
热议问题