qaztype
qaztype

Reputation: 73

Is it right to modify python's built-in code?

I have thought that maybe (if this does not mean any inconvenience) I could modify Python's built-in code so I could add my custom functions, modules, classes so I don't have to import anything when I am coding. Kind of considering building my own version.

I suppose this has disadvantages, I'd like to know which ones.

Upvotes: 0

Views: 224

Answers (2)

user4396006
user4396006

Reputation:

This is similar to an answer I wrote here:

Where is the PYTHONPATH defined in the first place?

That is not a good idea: you'd have to modify python's built-in code and libraries. I do not even know if they are compiled but if they weren't, even though you'd be able to modify the code (which I do not know if that is possible in the simple way we assume it) you would run into trouble:

  1. You may screw up any other part of the code by which python won't ever run and you won't ever know why
  2. Your application wouldn't be compatible with any other computer since you'd have to modify the package too.
  3. Even though you had a script modifying the package (so it is compatible) or even if you just ran it in a unique machine, you may not have enough system permissions to do so (in your case I suppose you do since I assume you are the machine's owner, but you may not)
  4. Most Python implementations do not have the raw source code available on-site. It's all compiled. So, you would need to go download the raw code and compile yourself, which is yet another problem.

I do not really recommend it, but if you still want to try, I hope someone may answer your question better than me.

I now add

Maybe if the modifications you want to add to the language are very consistent and so frequent that you can build your own version of Python that you can install anywhere and share with friends. As @user286474 pointed out there

It sounds like a proper Python environment has not been established .. don't go changing the tooling, change the environment. It's much simpler and will do what you need - the only reason to modify Python's core is to extended the language at a core level, such as adding new syntax or modifying how the VM works.-

I think he is right. Nevertheless, you may still want to modify the original content of Python to replace it with your custom stuff. I do not really recommend it. However, if you consider it appropriate, you can download the raw code of Python and compile it yourself. It is a hard but an interesting task.

Upvotes: 2

6502
6502

Reputation: 114579

I don't think that avoiding the need to import modules is a good reason for building your own python version. Note that you can write a module "standard.py" containing:

import sys, math, os, ... # all the other modules you like to have handy

and then in your code you can just add one line

import * from standard

and then you can use for example

sys.argv

as if you imported sys in your script

Upvotes: 1

Related Questions