-2

I am in doubt if a regular dict and an OrderedDict objects are truly interchangeable in a sense that the same function or method could return once a dict and other times an OrderedDict depending on the input arguments or for example in case of a class method depending on some other internal class instance attributes. If returning an OrderedDict would be significantly more costly than returning just a regular dict which should suffice as well why doing it the hard way? Would it be pythonic to create such a function or menthod? I use Python 2.7.

I have seen "Why should functions always return the same type?" and I felt my case is more special and less obvious to the unseasoned eye.

sszokoly
  • 64
  • 1
  • 7
  • If you are using Python 3.6, dictionaries are already ordered. Furthermore, don't start optimizing things, before you hit a performance barrier. https://stackoverflow.com/questions/39980323/dictionaries-are-ordered-in-python-3-6 – user1767754 Dec 29 '17 at 00:06
  • 1
    @user1767754 _"If you are using Python 3.6, dictionaries are already ordered."_ - Yes, that's true, but it's an implementation detail in Python 3.6 and shouldn't be relied on. Only in Python 3.7 are ordered dicts a language feature. – Christian Dean Dec 29 '17 at 00:09
  • Well, they aren't interchangeable if you care about insertion order, obviously. I'm not sure I understand your question. If you aren't, and a regular `dict` would do, then I agree, it seems you should just use that... – juanpa.arrivillaga Dec 29 '17 at 00:48
  • I do not care about the order under certain conditions and when I do not the function would return a simple dict which would be cheaper to create and populate. I simply wanted to find out if the experts would consider such a function bad coding, something to be frowned upon or still ok. – sszokoly Dec 29 '17 at 21:17

1 Answers1

0

Python uses duck typing. as long as an object presents the necessary interface required for an action, the type of object is irrelevant. An OrderedDict will present all the interfaces a dict would, so what the function returns is irrelevant. It'll be up to whatever uses what was returned. You should figure out the implementation of the use not the returning function.

What's your use case? Detailing that would help in suggesting an answer or alternatives.

kchawla-pi
  • 408
  • 5
  • 12
  • Thank you. In an extreme situation there would be only one key in the returned dictionary. Generating the value for that one key would be cheaper to do in a certain way than another way reusing another function which returns 'dict'. I am inclined to just return OrderedDict from all the functions involved even when there is no need for that.The purpose of this question was rather to find out if mixing dict and OrderedDict would still considered clean and acceptable. Not sure why I lost 2 credits, so far looks like this is not a valid question. :( – sszokoly Dec 29 '17 at 21:37
  • I hope i answered your question. I don't know how much expensive generating dict vs ordered dict would be. There is a small overhead but usually it's trivial. You should write and benchmark both versions and see if the code verbosity is worth the computation time saved. – kchawla-pi Jan 02 '18 at 07:54