1

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'

  • Does this answer your question? [What is the meaning of single and double underscore before an object name?](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name) – Russ J Jul 22 '21 at 14:24
  • thanks for the suggestion. But it doesn't. there it is explained only for variable and class [ NOT for methods ] – Shambhav Agrawal Jul 22 '21 at 14:26
  • 2
    there is a [clean-up at the end of the file](https://github.com/python/cpython/blob/c05a790693b3b00ac7cb5b96ad416ca9d8ecb14a/Lib/datetime.py#L2510); basically `_cmp` is removed before you can import it. – FObersteiner Jul 22 '21 at 14:56
  • Yes, you are right!! Thank you so much !! – Shambhav Agrawal Jul 22 '21 at 15:21

0 Answers0