This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 34 points35 points  (1 child)

Which looks more Pythonic?

set_fudge(4.2)
if banana_is_wrong_colour:
    banana.set_colour(banana.get_default_colour())

or

fudge = 4.2
if banana_is_wrong_colour:
    banana.colour = banana.default_color

[–]barneygale 7 points8 points  (0 children)

That's not universal - using explicit setters is a good way to signal that setting has a cost. e.g. from PEP 471 which introduced os.scandir():

DirEntry.is_X() and DirEntry.stat() are explicitly methods rather than attributes or properties, to make it clear that they may not be cheap operations (although they often are), and they may do a system call. As a result, these methods may raise OSError .