is there a way i can use methods whose name start with underscore in python

69 Views Asked by At

Refer code for datetime python module

"""Concrete date/time and related types.
See http://www.iana.org/time-zones/repository/tz-link.html for
time zone and DST data sources.
"""

__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo",
           "MINYEAR", "MAXYEAR")


import time as _time
import math as _math
import sys

def _cmp(x, y):
    return 0 if x == y else 1 if x > y else -1

if I want to use "_cmp(x, y). How can I??

example

consider Python class_file.py

def public_api():
    print ("public api")
  
def _private_api():
    print ("private API")

Calling file from Prompt

>>> from class_file import *
>>> public_api()
public api
  
>>> _private_api()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_private_api' is not defined
  
>>> import class_file
>>> class_file.public_api()
public api
>>> class_file._private_api()
private API

in the above case, if I use

print(datetime._cmp(1, 2))

it says AttributeError: module 'datetime' has no attribute '_cmp'

0

There are 0 best solutions below