In [1]:
from __future__ import print_function

In [2]:
'hello world #%r' % (3.14159, )


Out[2]:
'hello world #3.14159'

In [3]:
a = 3.1415926
str(a), repr(a)


Out[3]:
('3.1415926', '3.1415926')

In [4]:
a = 'ehllo'
str(a), repr(a)


Out[4]:
('ehllo', "'ehllo'")

In [5]:
class foo:
    '''
    Hi, this is the documentation of this class.

    This class is rather empty.
    '''
    pass

In [6]:
foo


Out[6]:
<class __main__.foo at 0xb66bff8c>

In [7]:
repr(foo)


Out[7]:
'<class __main__.foo at 0xb66bff8c>'

In [8]:
goo = foo()
goo


Out[8]:
<__main__.foo instance at 0xb4e744ac>

In [9]:
goo.hello = 'world'

In [10]:
dir(goo)


Out[10]:
['__doc__', '__module__', 'hello']

In [11]:
dir(foo)


Out[11]:
['__doc__', '__module__']

In [12]:
goo


Out[12]:
<__main__.foo instance at 0xb4e744ac>

In [13]:
help(foo)


Help on class foo in module __main__:

class foo
 |  Hi, this is the documentation of this class.
 |  
 |  This class is rather empty.


In [14]:
foo.__doc__


Out[14]:
'\n    Hi, this is the documentation of this class.\n\n    This class is rather empty.\n    '

In [15]:
a = 3
a


Out[15]:
3

In [16]:
def hoo(a, b):
    return a * b

In [17]:
hoo(3, 5)


Out[17]:
15

In [18]:
hoo('hello', 3)


Out[18]:
'hellohellohello'

In [19]:
'1234' * '3'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-9ca5afdc1cee> in <module>()
----> 1 '1234' * '3'

TypeError: can't multiply sequence by non-int of type 'str'

In [20]:
class foo:
    '''
    This is my official documentation for this stupid class.
    blah blah blah

        lasdjflasdjasd fasf as dfads fa dsf asdf

    lasdkjfaldsfsd fa dfa sd asdf a
    '''
    a = 4

In [21]:
dir(foo)


Out[21]:
['__doc__', '__module__', 'a']

In [22]:
foo.a


Out[22]:
4

In [23]:
a


Out[23]:
3

In [24]:
foo.__doc__


Out[24]:
'\n    This is my official documentation for this stupid class.\n    blah blah blah\n\n        lasdjflasdjasd fasf as dfads fa dsf asdf\n\n    lasdkjfaldsfsd fa dfa sd asdf a\n    '

In [25]:
foo.__module__


Out[25]:
'__main__'

In [26]:
foo.doc = 'no problem'

In [27]:
dir(foo)


Out[27]:
['__doc__', '__module__', 'a', 'doc']

In [28]:
foo.__doc__


Out[28]:
'\n    This is my official documentation for this stupid class.\n    blah blah blah\n\n        lasdjflasdjasd fasf as dfads fa dsf asdf\n\n    lasdkjfaldsfsd fa dfa sd asdf a\n    '

In [29]:
foo.doc


Out[29]:
'no problem'

In [30]:
foo.module = 1234

In [31]:
dir(foo)


Out[31]:
['__doc__', '__module__', 'a', 'doc', 'module']

In [32]:
foo.module


Out[32]:
1234

In [33]:
foo.__module__


Out[33]:
'__main__'

In [34]:
goo = foo()

In [35]:
dir(goo)


Out[35]:
['__doc__', '__module__', 'a', 'doc', 'module']

In [36]:
goo.a


Out[36]:
4

In [37]:
goo.b = 'laksjdf'

In [38]:
dir(goo)


Out[38]:
['__doc__', '__module__', 'a', 'b', 'doc', 'module']

In [39]:
goo.b


Out[39]:
'laksjdf'

In [40]:
goo.a


Out[40]:
4

In [41]:
goo.__dict__


Out[41]:
{'b': 'laksjdf'}

In [42]:
foo.__dict__


Out[42]:
{'__doc__': '\n    This is my official documentation for this stupid class.\n    blah blah blah\n\n        lasdjflasdjasd fasf as dfads fa dsf asdf\n\n    lasdkjfaldsfsd fa dfa sd asdf a\n    ',
 '__module__': '__main__',
 'a': 4,
 'doc': 'no problem',
 'module': 1234}

In [43]:
goo.a = 5000

In [44]:
goo.__dict__


Out[44]:
{'a': 5000, 'b': 'laksjdf'}

In [45]:
foo.a


Out[45]:
4

In [46]:
a


Out[46]:
3

In [47]:
hoo = foo()
hoo.a = 6000

In [48]:
print(a, foo.a, goo.a, hoo.a)


3 4 5000 6000

In [49]:
class Employee:
    '''
    first_name, last_name, hourly_rate, hours_worked
    '''
    def __init__(self, first_name, last_name, hourly_rate):
        self.first_name = first_name
        self.last_name = last_name
        self.hourly_rate = hourly_rate
        self.hours_worked = 0
    
    def add_time(self, time):
        self.hours_worked += time
        
    def __str__(self):
        return 'Mr. %s %s makes $%d per hour.' % (self.first_name, self.last_name, self.hourly_rate)

In [50]:
Employee.__dict__


Out[50]:
{'__doc__': '\n    first_name, last_name, hourly_rate, hours_worked\n    ',
 '__init__': <function __main__.__init__>,
 '__module__': '__main__',
 '__str__': <function __main__.__str__>,
 'add_time': <function __main__.add_time>}

In [51]:
bob = Employee('Robert', 'Fripp', 15.00)

In [52]:
bob.__dict__


Out[52]:
{'first_name': 'Robert',
 'hourly_rate': 15.0,
 'hours_worked': 0,
 'last_name': 'Fripp'}

In [53]:
bob.__doc__


Out[53]:
'\n    first_name, last_name, hourly_rate, hours_worked\n    '

In [54]:
bob.first_name


Out[54]:
'Robert'

In [55]:
bob.last_name


Out[55]:
'Fripp'

In [56]:
bob.add_time(5.00)

In [57]:
bob.__dict__


Out[57]:
{'first_name': 'Robert',
 'hourly_rate': 15.0,
 'hours_worked': 5.0,
 'last_name': 'Fripp'}

In [58]:
bob.add_time(1.23)
bob.__dict__


Out[58]:
{'first_name': 'Robert',
 'hourly_rate': 15.0,
 'hours_worked': 6.23,
 'last_name': 'Fripp'}

In [59]:
customer_number_9 = Employee('Evan', 'Wagner', 75.00)
customer_number_9.__dict__


Out[59]:
{'first_name': 'Evan',
 'hourly_rate': 75.0,
 'hours_worked': 0,
 'last_name': 'Wagner'}

In [60]:
customer_number_9.add_time(123)
customer_number_9.__dict__


Out[60]:
{'first_name': 'Evan',
 'hourly_rate': 75.0,
 'hours_worked': 123,
 'last_name': 'Wagner'}

In [61]:
bob.__dict__


Out[61]:
{'first_name': 'Robert',
 'hourly_rate': 15.0,
 'hours_worked': 6.23,
 'last_name': 'Fripp'}

In [62]:
class Customer:
    '''
    first_name, last_name
    '''
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

In [63]:
jack = Customer('jack', 'sprat')

In [64]:
jack.__dict__


Out[64]:
{'first_name': 'jack', 'last_name': 'sprat'}

In [65]:
str(jack)


Out[65]:
'<__main__.Customer instance at 0xb2aa302c>'

In [66]:
str(bob)


Out[66]:
'Mr. Robert Fripp makes $15 per hour.'

In [67]:
str(customer_number_9)


Out[67]:
'Mr. Evan Wagner makes $75 per hour.'

In [68]:
def get_earnings(self):
    return self.hourly_rate * self.hours_worked
Employee.get_earnings = get_earnings
Employee.__dict__


Out[68]:
{'__doc__': '\n    first_name, last_name, hourly_rate, hours_worked\n    ',
 '__init__': <function __main__.__init__>,
 '__module__': '__main__',
 '__str__': <function __main__.__str__>,
 'add_time': <function __main__.add_time>,
 'get_earnings': <function __main__.get_earnings>}

In [69]:
bob.__dict__, bob.get_earnings()


Out[69]:
({'first_name': 'Robert',
  'hourly_rate': 15.0,
  'hours_worked': 6.23,
  'last_name': 'Fripp'},
 93.45)

In [70]:
customer_number_9.__dict__, customer_number_9.get_earnings()


Out[70]:
({'first_name': 'Evan',
  'hourly_rate': 75.0,
  'hours_worked': 123,
  'last_name': 'Wagner'},
 9225.0)