Python @property versus method performance - which one to use?

后端 未结 6 1509
孤独总比滥情好
孤独总比滥情好 2021-01-04 03:31

I have written some code that uses attributes of an object:

class Foo:
    def __init__(self):
        self.bar = \"baz\"
myFoo = Foo()
print (myFoo.bar)
         


        
6条回答
  •  忘掉有多难
    2021-01-04 04:23

    If it's logically a property/attribute of the object, I'd say keep it as a property. If it's likely to become parametrised, by which I mean you may want to invoke myFoo.bar(someArgs) then bite the bullet now and make it a method.

    Under most circumstances, performance is unlikely to be an issue.

提交回复
热议问题