In [1]:
from __future__ import print_function
In [2]:
'hello world #%r' % (3.14159, )
Out[2]:
In [3]:
a = 3.1415926
str(a), repr(a)
Out[3]:
In [4]:
a = 'ehllo'
str(a), repr(a)
Out[4]:
In [5]:
class foo:
'''
Hi, this is the documentation of this class.
This class is rather empty.
'''
pass
In [6]:
foo
Out[6]:
In [7]:
repr(foo)
Out[7]:
In [8]:
goo = foo()
goo
Out[8]:
In [9]:
goo.hello = 'world'
In [10]:
dir(goo)
Out[10]:
In [11]:
dir(foo)
Out[11]:
In [12]:
goo
Out[12]:
In [13]:
help(foo)
In [14]:
foo.__doc__
Out[14]:
In [15]:
a = 3
a
Out[15]:
In [16]:
def hoo(a, b):
return a * b
In [17]:
hoo(3, 5)
Out[17]:
In [18]:
hoo('hello', 3)
Out[18]:
In [19]:
'1234' * '3'
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]:
In [22]:
foo.a
Out[22]:
In [23]:
a
Out[23]:
In [24]:
foo.__doc__
Out[24]:
In [25]:
foo.__module__
Out[25]:
In [26]:
foo.doc = 'no problem'
In [27]:
dir(foo)
Out[27]:
In [28]:
foo.__doc__
Out[28]:
In [29]:
foo.doc
Out[29]:
In [30]:
foo.module = 1234
In [31]:
dir(foo)
Out[31]:
In [32]:
foo.module
Out[32]:
In [33]:
foo.__module__
Out[33]:
In [34]:
goo = foo()
In [35]:
dir(goo)
Out[35]:
In [36]:
goo.a
Out[36]:
In [37]:
goo.b = 'laksjdf'
In [38]:
dir(goo)
Out[38]:
In [39]:
goo.b
Out[39]:
In [40]:
goo.a
Out[40]:
In [41]:
goo.__dict__
Out[41]:
In [42]:
foo.__dict__
Out[42]:
In [43]:
goo.a = 5000
In [44]:
goo.__dict__
Out[44]:
In [45]:
foo.a
Out[45]:
In [46]:
a
Out[46]:
In [47]:
hoo = foo()
hoo.a = 6000
In [48]:
print(a, foo.a, goo.a, hoo.a)
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]:
In [51]:
bob = Employee('Robert', 'Fripp', 15.00)
In [52]:
bob.__dict__
Out[52]:
In [53]:
bob.__doc__
Out[53]:
In [54]:
bob.first_name
Out[54]:
In [55]:
bob.last_name
Out[55]:
In [56]:
bob.add_time(5.00)
In [57]:
bob.__dict__
Out[57]:
In [58]:
bob.add_time(1.23)
bob.__dict__
Out[58]:
In [59]:
customer_number_9 = Employee('Evan', 'Wagner', 75.00)
customer_number_9.__dict__
Out[59]:
In [60]:
customer_number_9.add_time(123)
customer_number_9.__dict__
Out[60]:
In [61]:
bob.__dict__
Out[61]:
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]:
In [65]:
str(jack)
Out[65]:
In [66]:
str(bob)
Out[66]:
In [67]:
str(customer_number_9)
Out[67]:
In [68]:
def get_earnings(self):
return self.hourly_rate * self.hours_worked
Employee.get_earnings = get_earnings
Employee.__dict__
Out[68]:
In [69]:
bob.__dict__, bob.get_earnings()
Out[69]:
In [70]:
customer_number_9.__dict__, customer_number_9.get_earnings()
Out[70]: