Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: string method to_file #70792

Closed
nickeubank mannequin opened this issue Mar 21, 2016 · 6 comments
Closed

Feature request: string method to_file #70792

nickeubank mannequin opened this issue Mar 21, 2016 · 6 comments
Labels
topic-IO type-feature A feature request or enhancement

Comments

@nickeubank
Copy link
Mannequin

nickeubank mannequin commented Mar 21, 2016

BPO 26605
Nosy @vstinner, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2016-03-22.07:58:11.741>
created_at = <Date 2016-03-21.17:00:15.303>
labels = ['type-feature', 'invalid', 'expert-IO']
title = 'Feature request: string method `to_file`'
updated_at = <Date 2016-03-22.07:58:11.739>
user = 'https://bugs.python.org/nickeubank'

bugs.python.org fields:

activity = <Date 2016-03-22.07:58:11.739>
actor = 'SilentGhost'
assignee = 'none'
closed = True
closed_date = <Date 2016-03-22.07:58:11.741>
closer = 'SilentGhost'
components = ['IO']
creation = <Date 2016-03-21.17:00:15.303>
creator = 'nickeubank'
dependencies = []
files = []
hgrepos = []
issue_num = 26605
keywords = []
message_count = 6.0
messages = ['262141', '262142', '262143', '262144', '262155', '262169']
nosy_count = 4.0
nosy_names = ['vstinner', 'SilentGhost', 'serhiy.storchaka', 'nickeubank']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue26605'
versions = []

@nickeubank
Copy link
Mannequin Author

nickeubank mannequin commented Mar 21, 2016

As a social scientists trying to help other social scientists move from language like R, Stata, and Matlab into Python, one of the behaviors I've found unnecessarily difficult to explain is the "file.open()/file.close()" idiom (or, alternatively, context managers). In normal operating systems, and many high level languages, saving is a one-step operation.

I understand there are situations where an open file handle is useful, but it seems a simple to_file method on strings (essentially wrapping a context-manager) would be really nice, as it would save users from learning this idiom.

Any chance it's feasible? Sadly, I have no real C skills so can't implement myself. :(

@nickeubank nickeubank mannequin added topic-IO type-feature A feature request or enhancement labels Mar 21, 2016
@vstinner
Copy link
Member

It's easy to implement such function in a module and upload such module to PyPI (ex: "to_file" module :-)).

It's not easy to guess user expectations on such function: text encoding, buffering, etc.

Adding a method to the builtin str type which writes into a file is a no-no.

@serhiy-storchaka
Copy link
Member

Such function already exists in the stdlib! But with different order of arguments. See pathlib.Path.write_text().

Personally I'm very skeptical about usefulness of similar functions in the stdlib.

@vstinner
Copy link
Member

@nickeubank
Copy link
Mannequin Author

nickeubank mannequin commented Mar 22, 2016

Seems like existence of a method in the stdlib (or at least in the pathlib part of the stdlib) suggests issues with text encoding can be overcome.

IMHO I would suggest most people have no idea about that method (I've been doing python for years and this has always been a personal frustration, and I've asked several others for better options and no one had one to offer), and it seems like it would make much more sense as a string method. If someone has a string they want to save to disk, I can't imagine anyone looking in the Path library.

I respect the desire to avoid bloat -- the context manager or open/close idiom has just felt unnecessarily complicated (dare I say unpythonic?) for a common task.

Also, I think it's one that data scientists use a lot when they generate outputs to pull into LaTeX documents.

@SilentGhost
Copy link
Mannequin

SilentGhost mannequin commented Mar 22, 2016

Those methods were added in 3.5, so no wonder many people would be unaware of them. I would suggest first taking such a proposal to python-ideas, since bug tracker is not a venue for proposing/advocating such sweeping changes to the language.

@SilentGhost SilentGhost mannequin closed this as completed Mar 22, 2016
@SilentGhost SilentGhost mannequin added the invalid label Mar 22, 2016
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-IO type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants