0
Prototype in Python3
Hi! How can I add functions to an existing class? I want to recreate something like this: (JS) String.prototype.red = () => {...} How can I add a method to the str class?
2 Answers
+ 4
Python inheritance model is class based not prototype based like JavaScript. So your question should be how you can extend a data class in Python. The answer to that is INHERITANCE.
class A (B):
# A extends B
It is more tricky with str because str is not a Python type. It is implemented in C. It is a special case constructor.
Your best bet is composition. You may store the str object as a hidden field(double underscores) for your new data type and indirectly work with it.
+ 1
Ore Thank you
Hot today
Python — File Handling
2 Votes
Help me
0 Votes
What’s wrong?
2 Votes
Question is write a c program to print prime numbers up to n and print the largest number in array.
1 Votes
Achievements on Sololearn
1 Votes
How to draw in the console?
0 Votes