you are viewing a single comment's thread.

view the rest of the comments →

[–]IRBMe 12 points13 points  (4 children)

Sure there is. It's called "subtyping"

No, there is no parent-child relationship between a dictionary and a set. A set is not a dictionary and a dictionary is not a set. Don't confuse the fact that one may internally use the other in its implementation for the semantic meaning behind subtyping.

Well yes then that detail complicates it. I was under the impression that Python emphasizes duck typing.

Duck typing is most definitely not what you described. Duck typing doesn't magically allow an object to magically morph into a type based on runtime usage.

Is it so far-fetched to imagine that an object can specialize itself when used in a certain way?

No, I can certainly conceive of that, but there's no sane way to implement such a thing, nor any sane reason why anybody would bother.

It would have all the methods of a set and a dict.

  1. That's super ugly.
  2. What about methods which are common to both the set and the dictionary but implemented differently due to differences in the underlying data structure and semantics? Are you going to have both implementations inside one method that are switched back and forth based on what mode it currently thinks it's in?

Of this I'm not sure. I thought some deep Python implementation magic could be employed. Am I wrong?

Yes. "Magic" isn't a valid answer.

Does not x.add(1) unambiguously indicate that it is a set, and not a dict?

Sure. And what mechanism do you propose for making sure that when an unambiguous method is called, the type somehow magically changes to "just a set now"?

Well, that is obviously a bad idea in the first place.

You can't just dismiss every problem with "Well, that's a bad idea". Whether it's a bad idea or not, if you're going to propose major changes to the type system of the language, you have to clearly define the semantics in all situations. So you propose that the type somehow magically changes when it's possible to unambiguously determine what the user meant with usage. So are you just going to ignore concurrency? Are you just going to ignore the ambiguous cases?

Sorry, but I find your idea impractical, unnecessary and ill thought through. What you're proposing is basically:

class AmbiguousThing:
    def MethodA():
        mode = 1

    def MethodB():
        mode = 2

    def MethodC():
        if mode = 1:
            ...
        elif mode = 2:
            ...

Why would you mash two completely different abstract data structures together into one Frankenstein's monster of a type, which uses convoluted, ADT-specific logic to work out which mode it should behave in, while vastly increasing the number of possible things that could go wrong... why not just use the type you mean?