hello

A simple hello world example in nbdev.

source

say_hello

 say_hello (to)

Say hello to somebody

say_hello('Salma')
'Hello Salma!'

source

say_goodbye

 say_goodbye (to)

Say goodbye to somebody

say_goodbye('Menna')
'Goodbye Menna'
p = Person('Salma', 3)
p
Name: Salma, Age:3

source

Person

 Person (name:str, age:int)

A dummy class to test.

We can make the documentation nicer by adding a comment to each parameter that then get nicely rendered by nbdev

Person2('Menna', 38)
Name: Menna, Age:38

source

Person2

 Person2 (name:str, age:int)

A dummy class to test.

Type Details
name str a person’s name
age int a person’s age

To see where some func / obj comes from:

Signature:
show_doc(
    sym,
    renderer=None,
    name: 'str | None' = None,
    title_level: 'int' = 3,
)
Docstring: Show signature and docstring for `sym`
File:      ~/workspace/envs/claude-env/lib/python3.13/site-packages/nbdev/showdoc.py
Type:      function
# or see source code
say_hello??
Signature: say_hello(to)
Source:   
def say_hello(to):
    "Say hello to somebody"
    return f"Hello {to}!"
File:      /var/folders/9p/5ycxgmps44zc3f0y6vljhp2h0000gn/T/ipykernel_46397/180399582.py
Type:      function

In other languages like C++, u can define methods outside their class. In Python, we can use a util from fastcore to do so:

This gives us a @patch decorator


source

Person.__eq__

 Person.__eq__ (other:__main__.Person)

Return self==value.

Person('Salma', 3) == Person('Salma', 3)
True
Person('Salma', 3) == Person('Salma', 4)
False

Why do this?

This allows us to group same functionalities together eg all comparison operators in one place

Comparison Operators:


source

Person.__lt__

 Person.__lt__ (other:__main__.Person)

Return self<value.

assert Person('Salma', 3) < Person('Salma', 4)

source

Person.__gt__

 Person.__gt__ (other:__main__.Person)

Return self>value.

assert Person('Salma', 3) > Person('Aya', 3)