In [1]:
s = 'abc'
In [2]:
s.upper()
Out[2]:
In [3]:
# L E G B
# local
# enclosing
# global
# builtins
globals()
Out[3]:
In [4]:
globals()['s']
Out[4]:
In [5]:
s.upper()
Out[5]:
In [6]:
dir(s)
Out[6]:
In [7]:
s.title()
Out[7]:
In [8]:
x = 'this is a bunch of words to show to people'
x.title()
Out[8]:
In [9]:
for attrname in dir(s):
print attrname, s.attrname
In [11]:
for attrname in dir(s):
print attrname, getattr(s, attrname)
In [12]:
s.upper
Out[12]:
In [13]:
getattr(s, 'upper')
Out[13]:
In [15]:
while True:
attrname = raw_input("Enter attribute name: ").strip()
if not attrname: # if I got an empty string
break
elif attrname in dir(s):
print getattr(s, attrname)
else:
print "I don't know what {} is".format(attrname)
In [16]:
s.upper
Out[16]:
In [17]:
s.upper()
Out[17]:
In [18]:
5()
In [19]:
s.upper.__call__
Out[19]:
In [20]:
hasattr(s, 'upper')
Out[20]:
In [21]:
import sys
In [22]:
sys.version
Out[22]:
In [23]:
sys.version = '4.0.0'
In [24]:
sys.version
Out[24]:
In [25]:
def foo():
return 5
foo.x = 100
In [26]:
def hello(name):
return "Hello, {}".format(name)
In [27]:
hello('world')
Out[27]:
In [28]:
hello(123)
Out[28]:
In [29]:
hello(hello)
Out[29]:
In [30]:
class Foo(object):
def __init__(self, x):
self.x = x
def __add__(self, other):
return Foo(self.x + other.x)
In [31]:
f = Foo(10)
In [32]:
f.x
Out[32]:
In [33]:
class Foo(object):
pass
In [34]:
f = Foo()
f.x = 100
f.y = {'a':1, 'b':2, 'c':3}
In [35]:
vars(f)
Out[35]:
In [36]:
g = Foo()
g.a = [1,2,3]
g.b = 'hello'
In [37]:
vars(g)
Out[37]:
In [38]:
class Foo(object):
def __init__(self, x, y):
self.x = x
self.y = y
f = Foo(10, [1,2,3])
vars(f)
Out[38]:
In [51]:
class Person(object):
population = 0
def __init__(self, name):
self.name = name
Person.population = self.population + 1
def hello(self):
return "Hello, {}".format(self.name)
print "population = {}".format(Person.population)
p1 = Person('name1')
p2 = Person('name2')
print "population = {}".format(Person.population)
print "p1.population = {}".format(p1.population)
print "p2.population = {}".format(p2.population)
print p1.hello()
In [46]:
p1.thing
In [47]:
Person.thing = 'hello'
p1.thing
Out[47]:
In [54]:
class Person(object):
def __init__(self, name):
self.name = name
def hello(self):
return "Hello, {}".format(self.name)
class Employee(Person):
def __init__(self, name, id_number):
Person.__init__(self, name)
self.id_number = id_number
e = Employee('emp1', 1)
e.hello()
Out[54]:
In [55]:
e.hello()
Out[55]:
In [56]:
Person.hello(e)
Out[56]:
In [57]:
s = 'abc'
s.upper()
Out[57]:
In [58]:
str.upper(s)
Out[58]:
In [59]:
type(s)
Out[59]:
In [60]:
id(s)
Out[60]:
In [61]:
type(Person.hello)
Out[61]:
In [62]:
id(Person.hello)
Out[62]:
In [63]:
id(Person.hello)
Out[63]:
In [64]:
id(Person.hello)
Out[64]:
In [65]:
Person.__dict__
Out[65]:
In [67]:
Person.__dict__['hello'](e)
Out[67]:
In [68]:
# descriptor protocol
In [69]:
class Thermostat(object):
def __init__(self):
self.temp = 20
t = Thermostat()
t.temp = 100
In [70]:
t.temp = 0
In [73]:
class Thermostat(object):
def __init__(self):
self._temp = 20 # now it's private!
@property
def temp(self):
print "getting temp"
return self._temp
@temp.setter
def temp(self, new_temp):
print "setting temp"
if new_temp > 35:
print "Too high!"
new_temp = 35
elif new_temp < 0:
print "Too low!"
new_temp = 0
self._temp = new_temp
t = Thermostat()
t.temp = 100
print t.temp
t.temp = -40
print t.temp
In [78]:
# Temp will be a descriptor!
class Temp(object):
def __get__(self, obj, objtype):
return self.temp
def __set__(self, obj, newval):
if newval > 35:
newval = 35
if newval < 0:
newval = 0
self.temp = newval
class Thermostat(object):
temp = Temp() # temp is a class attribute, instance of Temp
t1 = Thermostat()
t2 = Thermostat()
t1.temp = 100
t2.temp = 20
print t1.temp
print t2.temp
In [81]:
# Temp will be a descriptor!
class Temp(object):
def __init__(self):
self.temp = {}
def __get__(self, obj, objtype):
return self.temp[obj]
def __set__(self, obj, newval):
if newval > 35:
newval = 35
if newval < 0:
newval = 0
self.temp[obj] = newval
class Thermostat(object):
temp = Temp() # temp is a class attribute, instance of Temp
t1 = Thermostat()
t2 = Thermostat()
t1.temp = 100
t2.temp = 20
print t1.temp
print t2.temp
In [ ]: