Skip to content

Instantly share code, notes, and snippets.

@darien-schettler
Last active March 6, 2020 14:22
Show Gist options
  • Save darien-schettler/2e138540a71b58dded6a66923658dc2f to your computer and use it in GitHub Desktop.
Save darien-schettler/2e138540a71b58dded6a66923658dc2f to your computer and use it in GitHub Desktop.
Function to retrieve an objects methods & respective documentation.
def see_object_methods_and_docs(obj):
""" Function to retrieve an objects methods & respective docs
Args:
obj (object): The object instantiated from the class for
which we would like to reveal information regarding.
i.e. If the class is `Cat` and the object is `jimmy_the_cat`
than we pass `jimmy_the_cat` into this function to reveal
that the `Cat` class has the `meow` method and also the
related documentation for the `meow` method.
- This related documentation is usually the following:
-- Args, Returns, Raises, and a Description of
the basic method functionality. While these
are the usually returned items... whatever
the __doc__ method dictates will be displayed.
"""
for i, attr in enumerate([x for x in dir(obj) if x[0]!="_"]):
print("\t {}.\t>>> {} <<<" \
"\n" \
"\t---------------------------------------------------\n" \
"{}" \
"\n\t---------------------------------------------------" \
"\n\n\n" \
"".format(i+1,
attr,
getattr(obj, str(attr)).__doc__))
@darien-schettler
Copy link
Author

darien-schettler commented Mar 6, 2020

The following is an example displaying the expected functionality for the integer class native to Python .


INPUT

>>> x = 5
>>> see_object_methods_and_docs(obj=x)

OUTPUT


1.	>>> bit_length <<<
	---------------------------------------------------
Number of bits necessary to represent self in binary.

>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
	---------------------------------------------------



	  2.	>>> conjugate <<<
	---------------------------------------------------
Returns self, the complex conjugate of any int.
	---------------------------------------------------



	  3.	>>> denominator <<<
	---------------------------------------------------
int([x]) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
	---------------------------------------------------



	  4.	>>> from_bytes <<<
	---------------------------------------------------
Return the integer represented by the given array of bytes.

  bytes
    Holds the array of bytes to convert.  The argument must either
    support the buffer protocol or be an iterable object producing bytes.
    Bytes and bytearray are examples of built-in objects that support the
    buffer protocol.
  byteorder
    The byte order used to represent the integer.  If byteorder is 'big',
    the most significant byte is at the beginning of the byte array.  If
    byteorder is 'little', the most significant byte is at the end of the
    byte array.  To request the native byte order of the host system, use
    `sys.byteorder' as the byte order value.
  signed
    Indicates whether two's complement is used to represent the integer.
	---------------------------------------------------



	  5.	>>> imag <<<
	---------------------------------------------------
int([x]) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
	---------------------------------------------------



	  6.	>>> numerator <<<
	---------------------------------------------------
int([x]) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
	---------------------------------------------------



	  7.	>>> real <<<
	---------------------------------------------------
int([x]) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
	---------------------------------------------------



	  8.	>>> to_bytes <<<
	---------------------------------------------------
Return an array of bytes representing an integer.

  length
    Length of bytes object to use.  An OverflowError is raised if the
    integer is not representable with the given number of bytes.
  byteorder
    The byte order used to represent the integer.  If byteorder is 'big',
    the most significant byte is at the beginning of the byte array.  If
    byteorder is 'little', the most significant byte is at the end of the
    byte array.  To request the native byte order of the host system, use
    `sys.byteorder' as the byte order value.
  signed
    Determines whether two's complement is used to represent the integer.
    If signed is False and a negative integer is given, an OverflowError
    is raised.
	---------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment