API details.

functions

say_hello[source]

say_hello(to)

Say hello to somebody

examples

run function

say_hello("Wilbur")
'Hello Wilbur!'

make svg

from IPython.display import display,SVG
display(SVG('<svg height="100"><circle cx="50" cy="50" r="40"/></svg>'))

tests

assert say_hello("Jeremy")=="Hello Jeremy!"
assert say_hello("Chris")!="Hello Jeremy!"

classes

class HelloSayer[source]

HelloSayer(to)

Say hello to to using say_hello

HelloSayer.say[source]

HelloSayer.say()

Do the saying

examples

create instance

We can define an instance of the class HelloSayer by passing the appropriate parameters to the constructor (in this case a single string containing the object of prospective saying).

o = HelloSayer("Alexis")

It is possible to simply print the __dict__ associated to any instance of the object as:

print(o.__dict__)
{'to': 'Alexis'}

This works even if there is no __str__ method defined. Otherwise, it is probably better to use the internally defined __repr__ and __str__ as:

print([o])
print(o)
print(o.to)
o.say()
[to: Alexis]
<class '__main__.HelloSayer'>
to = Alexis
Alexis
'Hello Alexis!'

interrogate instance

If you just want a list of the methods associated to an object, you can use a list comprehension as:

def get_object_methods(object):
    return [method_name for method_name in dir(object)
                  if callable(getattr(object, method_name))]
print(get_object_methods(o))
['__class__', '__delattr__', '__dir__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'say']

If it is necessary to get a comprehensive listing of the contents of an object, use the dir function as:

dir(o)
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'say',
 'to']

It is also possible to use the python inspect module to get more detailed information about the structure of an object:

import inspect
inspect.getmembers(o)
[('__class__', __main__.HelloSayer),
 ('__delattr__',
  <method-wrapper '__delattr__' of HelloSayer object at 0x7f47460ef580>),
 ('__dict__', {'to': 'Alexis'}),
 ('__dir__', <function HelloSayer.__dir__()>),
 ('__doc__', 'Say hello to `to` using `say_hello`'),
 ('__eq__', <method-wrapper '__eq__' of HelloSayer object at 0x7f47460ef580>),
 ('__format__', <function HelloSayer.__format__(format_spec, /)>),
 ('__ge__', <method-wrapper '__ge__' of HelloSayer object at 0x7f47460ef580>),
 ('__getattribute__',
  <method-wrapper '__getattribute__' of HelloSayer object at 0x7f47460ef580>),
 ('__gt__', <method-wrapper '__gt__' of HelloSayer object at 0x7f47460ef580>),
 ('__hash__',
  <method-wrapper '__hash__' of HelloSayer object at 0x7f47460ef580>),
 ('__init__', <bound method HelloSayer.__init__ of to: Alexis>),
 ('__init_subclass__', <function HelloSayer.__init_subclass__>),
 ('__le__', <method-wrapper '__le__' of HelloSayer object at 0x7f47460ef580>),
 ('__lt__', <method-wrapper '__lt__' of HelloSayer object at 0x7f47460ef580>),
 ('__module__', '__main__'),
 ('__ne__', <method-wrapper '__ne__' of HelloSayer object at 0x7f47460ef580>),
 ('__new__', <function object.__new__(*args, **kwargs)>),
 ('__reduce__', <function HelloSayer.__reduce__()>),
 ('__reduce_ex__', <function HelloSayer.__reduce_ex__(protocol, /)>),
 ('__repr__', <bound method HelloSayer.__repr__ of to: Alexis>),
 ('__setattr__',
  <method-wrapper '__setattr__' of HelloSayer object at 0x7f47460ef580>),
 ('__sizeof__', <function HelloSayer.__sizeof__()>),
 ('__str__', <bound method HelloSayer.__str__ of to: Alexis>),
 ('__subclasshook__', <function HelloSayer.__subclasshook__>),
 ('__weakref__', None),
 ('say', <bound method HelloSayer.say of to: Alexis>),
 ('to', 'Alexis')]

You can also simply call the help function on your instance.

?help
Signature:   help(*args, **kwds)
Type:        _Helper
String form: Type help() for interactive help, or help(object) for help about object.
Namespace:   Python builtin
File:        /usr/lib/python3.8/_sitebuiltins.py
Docstring:  
Define the builtin 'help'.

This is a wrapper around pydoc.help that provides a helpful message
when 'help' is typed at the Python interactive prompt.

Calling help() at the Python prompt starts an interactive help session.
Calling help(thing) prints help for the python object 'thing'.
help(o)
Help on HelloSayer in module __main__ object:

class HelloSayer(builtins.object)
 |  HelloSayer(to)
 |  
 |  Say hello to `to` using `say_hello`
 |  
 |  Methods defined here:
 |  
 |  __init__(self, to)
 |      Define the object of the prospective saying
 |  
 |  __repr__(self)
 |      Represent instances for debugging
 |  
 |  __str__(self)
 |      Represent instances when printed
 |  
 |  say(self)
 |      Do the saying
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)