Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7719
| Date | 2011-06-15 19:00 -0700 |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Subject | os.path and Path |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8.1308188800.1164.python-list@python.org> (permalink) |
In my continuing quest for Python Mastery (and because I felt like it ;)
I decided to code a Path object so I could dispense with all the
os.path.join and os.path.split and os.path.splitext, etc., etc., and so
forth.
While so endeavoring a couple threads came back and had a friendly
little chat in my head:
Thread 1: "objects of different types compare unequal"
self: "nonsense! we have the power to say what happens in __eq__!"
Thread 2: "objects that __hash__ the same *must* compare __eq__!"
self: "um, what? ... wait, only immutable objects hash..."
Thread 2: "you're Path object is immutable..."
self: "argh!"
Here's the rub: I'm on Windows (yes, pity me...) but I prefer the
unices, so I'd like to have / seperate my paths. But I'm on Windows...
So I thought, "Hey! I'll just do some conversions in __eq__ and life
will be great!"
--> some_path = Path('/source/python/some_project')
--> some_path == '/source/python/some_project'
True
--> some_path == r'\source\python\some_project'
True
--> # if on a Mac
--> some_path == ':source:python:some_project'
True
--> # oh, and because I'm on Windows with case-insensitive file names...
--> some_path == '/source/Python/some_PROJECT'
True
And then, of course, the ghosts of threads past came and visited. For
those that don't know, the __hash__ must be the same if __eq__ is the
same because __hash__ is primarily a shortcut for __eq__ -- this is
important when you have containers that are relying on this behavior,
such as set() and dict().
So, I suppose I shall have to let go of my dreams of
--> Path('/some/path/and/file') == '\\some\\path\\and\\file'
and settle for
--> Path('...') == Path('...')
but I don't have to like it. :(
</whine>
~Ethan~
What, you didn't see the opening 'whine' tag? Oh, well, my xml isn't
very good... ;)
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-15 19:00 -0700
Re: os.path and Path Laurent Claessens <moky.math@gmail.com> - 2011-06-16 09:03 +0200
Re: os.path and Path Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-16 07:58 +0000
Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 09:16 -0700
Re: os.path and Path Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-16 16:41 +0000
Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 10:18 -0700
Re: os.path and Path Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-16 11:21 -0600
Re: os.path and Path Christian Heimes <lists@cheimes.de> - 2011-06-16 18:32 +0200
Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 10:07 -0700
Re: os.path and Path Chris Angelico <rosuav@gmail.com> - 2011-06-17 11:00 +1000
Re: os.path and Path Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-16 07:14 +0000
Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 09:05 -0700
Re: os.path and Path Chris Torek <nospam@torek.net> - 2011-06-17 00:48 +0000
Re: os.path and Path Ethan Furman <ethan@stoneleaf.us> - 2011-06-16 18:19 -0700
Re: os.path and Path Ned Deily <nad@acm.org> - 2011-06-16 19:55 -0700
Re: os.path and Path rusi <rustompmody@gmail.com> - 2011-06-16 21:24 -0700
csiph-web