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

Rob Gaddi rgaddi at highlandtechnology.invalid
Thu May 31 12:51:30 EDT 2018


On 05/31/2018 07:49 AM, Dan Strohl wrote:
> Is it possible to override the assignment of built in types to the shorthand representations?   And if not, is it a reasonable thought to consider adding?
> 
> For example, right now, if I do:
> 
> test = "this is a string",
> 
> I get back str("this is a string").  What if I want to return this as my_string("this is a string")  (OK, I know I have a recursive issue in my example, but hopefully you get the point).
> 
> Or;
> 
> Test = ['item1', 'item2', 'item3'] returns a list, what if I want to add functionality to all lists in my module?  (and yes, I know I could simply not do [] and always do my_list('item1', 'item2', 'item3']
> 
> 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.

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.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list