-
Notifications
You must be signed in to change notification settings - Fork 187
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
Path subclassing str #228
Path subclassing str #228
Conversation
…er support being immutable. the as syntax is supported for with statements.
… in some versions of Python)
By the way, to clarify my earlier statement, the only "changed" behavior is the following corner case: mycwd = local.cwd
with mycwd('newdir'):
print(mycwd) Under the old system, mycwd would be the 'newdir' directory, but the new system it will still print the old dir. It does not really track the current working directory even in the old system, as any other changes to cwd through other methods will not change it. The new behavior, IMO, is actually more intuitive; and the other recommended methods work as expected: with local.cwd('newdir'):
print(local.cwd) This prints the new cwd, since cwd is really a property that gets the cwd. If you want the old behavior in a more pythonic way, you can even do: with local.cwd('newdir') as mydir:
print(mydir) |
Just for comparison quote from https://www.python.org/dev/peps/pep-0428/#no-confusion-with-builtins:
|
The downside is that then Without The downside mentioned is that something like Open for discussion, though, at least before 1.6. |
|
For one, that breaks duck typing. You have to try and catch then retry a different open. This is a big, known problem, but last I heard people were saying it would be 'to hard' to find and fix every open function in Python's stdlib. I haven't seen much development on pathlib since it was introduced. Support would have to be added to more than open, too; any open function from the stdlib or 3rd party libraries like numpy and pandas would have to be patched too. A standard way to do it would make sense; the current way open works is it checks isinstance(str) and then tries a buffer interface if it doesn't work. I would rather see a special property I've been making the API's similar, but there are a few improvements that are not in pathlib. (I'm considering submitting a patch for one or two improvements to pathlib to see if there is any interest). Also, since pathlib doesn't support remotes, the two can never fully be merged. My hope is to have the api similar enough so that one can easily switch between them. |
Testing a possible fix, simply cashing the value and using that if chdir is not called. Will not allow the cd command to be run without using chdir directly, though |
This patch changes path to be a subclass of str, similar to unipath. Since path was always supposed to be immutable, this was a simple change. The only api change is that cwd behaves slightly differently; due to the latest changes with the way it's now generated when you call local.cwd, this should be completely transparent. (For example, all tests pass). You now can use the as clause of a with statement to grab the new path.
Because of this change, treating the path like a string now works as expected; you can use
.endswith
, etc., and you can even directly open a path without converting it to a string first.