Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 506127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:40:07+00:00 2026-05-13T06:40:07+00:00

I’ve got a few questions about best practices in Python. Not too long ago

  • 0

I’ve got a few questions about best practices in Python. Not too long ago I would do something like this with my code:

...
junk_block = "".join(open("foo.txt","rb").read().split())
...

I don’t do this anymore because I can see that it makes code harder to read, but would the code run slower if I split the statements up like so:

f_obj = open("foo.txt", "rb")
f_data = f_obj.read()
f_data_list = f_data.split()
junk_block = "".join(f_data_list)

I also noticed that there’s nothing keeping you from doing an ‘import’ within a function block, is there any reason why I should do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:40:08+00:00Added an answer on May 13, 2026 at 6:40 am

    As long as you’re inside a function (not at module top level), assigning intermediate results to local barenames has an essentially-negligible cost (at module top level, assigning to the “local” barenames implies churning on a dict — the module’s __dict__ — and is measurably costlier than it would be within a function; the remedy is never to have “substantial” code at module top level… always stash substantial code within a function!-).

    Python’s general philosophy includes “flat is better than nested” — and that includes highly “nested” expressions. Looking at your original example…:

    junk_block = "".join(open("foo.txt","rb").read().split())
    

    presents another important issues: when is that file getting closed? In CPython today, you need not worry — reference counting in practice does ensure timely closure. But most other Python implementations (Jython on the JVM, IronPython on .NET, PyPy on all sorts of backends, pynie on Parrot, Unladen Swallow on LLVM if and when it matures per its published roadmap, …) do not guarantee the use of reference counting — many garbage collection strategies may be involved, with all sort of other advantages.

    Without any guarantee of reference counting (and even in CPython it’s always been deemed an implementation artifact, not part of the language semantics!), you might be exhausting resources, by executing such “open but no close” code in a tight loop — garbage collection is triggered by scarcity of memory, and does not consider other limited resources such as file descriptors. Since 2.6 (and 2.5, with an “import from the future”), Python has a great solution via the RAII (“resource acquisition is initialization”) approach supported by the with statement:

    with open("foo.txt","rb") as f:
      junk_block = "".join(f.read().split())
    

    is the least-“unnested” way that will ensure timely closure of the file across all compliant versions of Python. The stronger semantics make it preferable.

    Beyond ensuring the correct, and prudent;-), semantics, there’s not that much to choose between nested and flattened versions of an expression such as this. Given the task “remove all runs of whitespace from the file’s contents”, I would be tempted to benchmark alternative approaches based on re and on the .translate method of strings (the latter, esp. in Python 2.*, is often the fastest way to delete all characters from a certain set!), before settling on the “split and rejoin” approach if it proves to be faster — but that’s really a rather different issue;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sometimes the device gets "out of sync" with eclipse and… May 14, 2026 at 10:32 pm
  • Editorial Team
    Editorial Team added an answer OK, so after a while digging around, I found that… May 14, 2026 at 10:32 pm
  • Editorial Team
    Editorial Team added an answer Inside a method body you can chain as many dos… May 14, 2026 at 10:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.