Empty class object in Python

后端 未结 4 1775
情深已故
情深已故 2020-12-15 02:31

I\'m teaching a Python class on object-oriented programming and as I\'m brushing up on how to explain classes, I saw an empty class definition:

class Employe         


        
4条回答
  •  没有蜡笔的小新
    2020-12-15 03:13

    Try with lambda:

    john.greet = lambda : print( 'hello world!' )
    

    The you'll be able to do:

    john.greet()
    

    EDIT: Thanks Thomas K for the note - this works on Python 3.2 and not for Python2, where print appeared to be statement. But this will work for lambdas, without statements (right? Sorry, I know only python3.2 (: )

提交回复
热议问题