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 8770533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:34:27+00:00 2026-06-13T17:34:27+00:00

I am not sure if this is ‘good python practice’, but would it be

  • 0

I am not sure if this is ‘good python practice’, but would it be possible to, say, define a custom File-object that could do something like:

myfile = myopen('myfile.txt')
with myfile:
    write('Hello World!') #notice we don't put "myfile.write(..)" here!

i.e the File-context creates a function “write()” so that we don’t have to type myfile.write(..) etc. It saves typing and makes the purpose clearer in some cases. For instance:

myboard = ChessBoard()
with ChessBoard():
    make_move("e4")
    make_move("e5")
    give_up()

as opposed to

myboard = ChessBoard()
with ChessBoard():
    make_move("e4",board=myboard)
    make_move("e5",board=myboard)
    give_up(board=myboard)

The question is: should I do this? and HOW can I do it? I am guessing I would have to modify the globals()-dict somehow, but that seems like a bad idea..

EDIT: Ok thanks! I got multiple good answers advising me not to do this. So I won’t do it :o)

  • 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-06-13T17:34:28+00:00Added an answer on June 13, 2026 at 5:34 pm

    This is not what context managers are for and, as has been remarked, I beats the “explicit is better than implicit” principle. The only way to make it work would have to work around Python’s compositional semantics, which are one of its strong points. What you can do to save typing, if there’s only a single method to be called multiple times, is:

    move = ChessBoard().make_move
    move("e4")
    move("e5")
    

    Or with multiple such methods:

    board = ChessBoard()
    move = board.make_move
    give_up = board.give_up
    # call methods
    

    (In FP terms, this is actually partial application, not currying.)

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

Sidebar

Related Questions

Not sure this is possible, but looking to write a script that would return
I'm not sure this is even possible, but I would really like to be
I'm not sure this is even possible but I know if it is, the
I'm not sure this is possible, but in ruby, you can dynamically call a
I'm not sure this is possible, but is there a syntax to be used
I'm not sure this is possible, but is there a way, using template programming
Hi I'm sure this is possible but am not sure how to go about
I'm not sure this is even possible, but I need a confirmation before doing
I'm sure this is possible some how, but what I'm doing is not working.
I'm not sure this is possible but I'm trying to declare an Object that

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.