Override built in types... possible? or proposal.

Dan Strohl D.Strohl at F5.com
Thu May 31 13:24:11 EDT 2018


> >
> > I am envisioning something in the header like an import statement
> > where I could do;
> >
> > override str=my_string
> > override list=my_list
> >
> > This would only be scoped to the current module and would not be
> imported when that module was imported.
> >
> > Thoughts?
> >
> > Dan Strohl
> >
> 
> My problem with this idea is that it breaks expectations.  If I know one thing as
> a Python programmer, it's that 'Bob' is a str.  Each time and every time.  If you
> could override the meaning of basic constant identifiers to where I have no
> idea how they behave, that creates an easy thing to miss that changes the
> entire meaning of the things you've written.
> 

True, though, to determine what almost anything is, you should look at the imports anyway, just in case I happened to do something like;

Import my_sys as sys

> What's the use case here?  And why is that use case better than, for instance,
> simply defining a function in the module that does the things you want done
> to strings?  Not everything has to be an object method.

It's not necessarily better, it simply provides more flexibility in how things are approached.  In most cases I would probably define a function for something as you suggested, or define a new class and just instantiate that  object instead when needed, but I can see a time when it would be nice to be able to simply say, "I really want to handle all of my dictionaries in this module in a certain way", then not have to worry about it.

To me, one of the things I like about Python is that I can override many of the way things are handled via sub-classes, magic methods, importing "as" etc... this is simply an extension of that existing flexibility.

And yes, it gives developers another tool they can shoot themselves pretty easily in the foot with if they aren't careful in how they use it, but so many of the good tools do that already.

Honestly, I am not so locked into this that I would scream about it not working, but there have been times when it would have been helpful in the past, so I figured I would bring it up and see what others thought.

Dan






More information about the Python-list mailing list