In [1]:
class Object:
pass
obj = Object()
In [2]:
type(obj)
Out[2]:
In [3]:
def square(num):
return num**2
In [4]:
type(square)
Out[4]:
In [5]:
type(Object)
Out[5]:
In [11]:
class Sample(object): # not clear why this "object" is here
pass
In [7]:
x = Sample()
In [8]:
type(x)
Out[8]:
In [35]:
class Dog(object):
# class object attribute
species = 'mammal'
def __init__(self, breed, name, fur=True):
# looks like you dont need to declare this vars in the class for
# them to be generated. neat.
self.breed = breed
self.name = name
self.fur = fur
def bark(self):
print("SAY WOOF AGAIN. I DARE YOU.")
In [36]:
sam = Dog(breed='lab', name='sammy', fur=False)
In [37]:
sam.bark()
In [52]:
class Circle(object):
pi = 3.14
def __init__(self, radius=1):
self.radius = radius
def area(self):
return (self.radius**2) * Circle.pi
def set_radius(self, new_radius):
self.radius = new_radius
In [49]:
c = Circle(20) # looks like you dont need to name the params, just use the right order?
c.area()
c.set_radius(100)
c.area()
Out[49]:
In [53]:
class Animal(object):
def __init__(self):
print "Animal Created"
def whoAmI(self):
print "Animal"
def eat(self):
print "Eating"
In [60]:
class Dog(Animal):
def __init__(self):
Animal.__init__(self) # interesting syntax
print "Dog Created"
def whoAmI(self):
print("Dog")
In [61]:
d = Dog()
In [62]:
d.whoAmI()
In [80]:
class Book(object):
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
#special functions
# this is the equiv of customstringconvertable
def __str__(self):
return "Title: {t}, Author: {a}, Pages: {pg}".format(t=self.title, a=self.author, pg=self.pages)
def __len__(self):
return self.pages
# deinit
def __del__(self):
print("Deleting %s!" % self.title)
In [77]:
b = Book(author="Louis",pages=25,title="Python Trials")
# so params don't need to be in the same order if they are named...
In [70]:
print(b)
In [75]:
len(b)
Out[75]:
In [78]:
del(b)
In [79]:
print(b)
In [ ]: