Skip to main content

Create private functions in Python

Project description

Private Function in Python

"A smarter way to create private functions in python"

from FAHEEM41

  • version: 1.5.23
  • first published in: Sololearn
  • Github first publishing date: 23rd July, 2022
  • Last Updated: 11th January, 2023


Why use the code?

Let's start with the concept of private functions. Private functions are functions that are only accessible inside the declared module, no other function from any other module can access it.
This project mainly focuses on the security issues of a code. This enables a way to restrict code from accessing other functions from other modules that were not meant to be accessed. And thus, it ensures that only non-private functions i.e. public functions can be accessed from anywhere outside the code.

Here's an example, suppose a module has two functions: add() and main(). main() function is meant to run and can be called from other modules. On the other hand, the add() function is a function that is only called by the main() function, and we want to keep this function private, i.e. ensure that it cannot be accessed or called from any other module.


Understanding the code

Here we have implemented a very naive idea to get our work done. We have used a decorator which will check whether the function is a private or public function and thereby deny or grant access respectively. The decorator will run, as usual, with the function called, and before the function ran.
For details understanding, have a look at the privatefunc.py file; the documentation of the code, along with how the code is working is given inside the source code.


How to use the code? (using pip)

In terminal type the command: pip install privatefunc
Add this lines in your module:
from privatefunc import PrivateFunc
private = PrivateFunc("nameOfThisModuleHere").private
Now add @private before the function you want to make private

For better understanding have a look at moduleWithPrivateFunc.py


Everything put together:

# example.py
from privatefunc import PrivateFunc
private = PrivateFunc("example").private

@private
def hello():
    pass

Sample Code

Go through the demo code to completely understand the insights of the source.


© 2021-2023 Md. Faheem Hossain fmhossain2941@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

privatefunc-1.6.23.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

privatefunc-1.6.23-py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page