Python – only one return per method? [duplicate]

I’m trying to sort out whether this is just a personal preference thing or whether it’s actually bad practice to do this one way or another. I did try to reference PEP8 first, and I did not see an answer.

What I’m talking about here is the use of more than one return statement. In these examples, it’s not such a big deal, as they are very short and easily read.

def foo(obj):
    if obj.condition:
        return True
    return False

def bar(obj):
    answer = False
    if obj.condition:
        answer = True
    return answer

It bugs me to assign a variable every time bar() is called, instead of just determining the answer and returning it. But in a longer function a second return might be hidden and so someone reading the code may have not realized that the function might return a value before hitting the end of the line:

def spam(obj):
    blurb = "Francis"
    if obj.condition:
        blurb = map(gormf, [z for z in porp(obj)])
        return blurb[2]
    elif not obj.condition and dreg(obj.jek):
        blurb = obj.hats * 17
        if blurb % 13: return blurb
    else:
        blurb = obj.name
    return "Whales and %s" blurb

That’s a terrible function, but I think you see what I’m poking at.

So is it preferable to create a function to have a single, clear exit – even if it’s a few more operations than you actually need? Or should one try to root this out whenever possible in favor of shorter code?

2

My personal preference is to allow multiple returns at the very beginning and the very end, but not in the middle (where they could be easily missed).

Of course, one might argue that if a subroutine is so long that it has a “beginning”, “middle” and “end”, it is too long anyway.

I am specifically fond of using the trailing Perl-style conditionals as sort-of “guard expressions” (example in pseudo-Ruby, but applies equally to other languages):

def sort(list)
  return []         if list.empty?
  return list.clone if list.size == 1
  ... actual sorting here
end

Or in Python:

def sort(list):
  if not list:       return []
  if list.size == 1: return list.clone
  ... actual sorting here
end

1

Setting aside the fact that “if true then true else false” is silly, one thing to keep in mind is that foo() only has two returns because if is a statement in Python, not an expression. In a functional language like Standard ML, where everything is an expression that returns a value, foo() would look like this:

fun foo (obj) =
    if obj.condition
    then True
    else False

So, conceptually, your foo() could be considered to have a single return.

Multiple returns starts to get confusing is when you combine them with side effects and looping statements. In that case in can get tricky to follow the execution of the function and determine whether it’ll abort early or not. But once that’s an issue, rewriting the function to be easier to follow and understand is far more important than adhering strictly to some style guide or another.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật