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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file privatefunc-1.6.23.tar.gz.

File metadata

  • Download URL: privatefunc-1.6.23.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for privatefunc-1.6.23.tar.gz
Algorithm Hash digest
SHA256 735485f05b495a45d33b44f74746161d9114da12f1fe827697c455944c4cd938
MD5 312edfd04da0cd7409d43a71f9519f20
BLAKE2b-256 3c0866381acafbf87b6a642b0cf0b2db4382f33cbd385a2096166b207613d4f6

See more details on using hashes here.

File details

Details for the file privatefunc-1.6.23-py3-none-any.whl.

File metadata

  • Download URL: privatefunc-1.6.23-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for privatefunc-1.6.23-py3-none-any.whl
Algorithm Hash digest
SHA256 ca243cad9722a51cf61f8639e52772b524de4e98ec5d0be0883eb0d07688e423
MD5 b6a6a126b39bab1996fdec6f3ee91ede
BLAKE2b-256 919e4ecb61364aeda48e58082f5c6b959225e5eac6383e4fa17fe78290eae6ff

See more details on using hashes here.

Supported by

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