class alien: # A property of all aliens appearance = 'ugly' ET = alien() Alf = alien() Yoda = alien() print(ET.appearance, Alf.appearance, Yoda.appearance) ET.color = 'red' ET.head = 'giant' ET.planet = 'Mars?' Alf.color = 'yellow' Alf.head = 'funny with an elephant trompe' Alf.planet = 'Melmac' Yoda.color = 'green' Yoda.head = 'wrinkled with long hairy ears' Yoda.planet = 'Jupitron' print('ET:',ET.appearance,ET.color, ET.planet, ET.head) print('Alf:',Alf.appearance,Alf.color, Alf.planet, Alf.head) print('Yoda:',Yoda.appearance,Yoda.color, Yoda.planet, Yoda.head) alien.appearance = 'handsome stallion' print(ET.appearance,Alf.appearance,Yoda.appearance) # Second version of the alien class class alienV2(object): # Class variable number = 0 # Initialization function def __init__(self, name, color, planet, head): # Instance variables self.name = name self.color = color self.planet = planet self.head = head alienV2.number += 1 # A class function def speak(self): print('My name is ' + self.name + ' from the planet ' + self.planet + '; my head is ' + self.head + ', and my skin gets more and more ' + self.color + ' when I tan in the sun. There are other ' + str(alienV2.number -1) + ' aliens like me hiding in this code.') print(' ') alien_1 = alienV2('ET','red','Mars','big') alien_2 = alienV2('Alf','yellow','Melmac','funny with an elephant trompe') alien_3 = alienV2('Yoda','green','Jupitron','wrinkled with long hairy ears') alien_3.speak() alienV2.number = 0 alien_list = [] alien_list.append(alienV2('ET','red','Mars','big')) alien_list.append(alienV2('Alf','yellow','Melmac','funny with like an elephant trompe')) alien_list.append(alienV2('Yoda','green','Jupitron','wrinkled with long hairy ears')) for i in range(len(alien_list)): alien_list[i].speak() class your_class(object): # Class variables here def __init__(self,instance_var): # Instance variables here (and in the init function) self.instance_var = instance_var # Define a functon or action for your object def action(self): # Do something, like: self.instance_var = 0 #define child class class child_class(parent_class): # initialization is not neccesary def __init__(self): print "Calling child constructor" def childMethod(self): print 'Calling child method' # Define a child class that has alienV2 as parent class %pylab inline from IPython.display import Image class alien_interactive(alienV2): # Initialize child class function def __init__(self, imgsrc, name, color, planet, head): super(alien_interactive, self).__init__(name, color, planet, head) self.imgsrc = imgsrc def show_yourself(self): img = Image(filename=self.imgsrc) display(img) self.speak() alien_list = [] alienV2.number = 0 # Note it's not alien_interactive.number alien_list.append(alien_interactive('images/ET.jpg','ET','red','Mars','big')) alien_list.append(alien_interactive('images/Alf.jpg','Alf','yellow','Melmac','funny with like an elephant trompe')) alien_list.append(alien_interactive('images/yoda.jpg','Yoda','green','Jupitron','wrinkled with long hairy ears')) alien_list[1].show_yourself() from IPython.html.widgets import interact def show_interactive(alien_number): alien_list[alien_number].show_yourself() interact(show_interactive, alien_number=(0,2)); class your_class_child(your_class): # Initialize child class function def __init__(self, child_instance_variable, parent_instance_variable): super(your_class_child, self).__init__(parent_instance_variable) # Define child instance variables self.child_instance_variable = child_instance_variable # Add an action unique to the child class def child_action(self): # Do something, like self.child_instance_variable = 0 # Define a child class that has alienV2 as parent class class alien_interactiveV2(alienV2): # Initialize child class function def __init__(self, imgsrc,*args): super(alien_interactiveV2, self).__init__(*args) self.imgsrc = imgsrc def show_yourself(self): try: img = Image(filename=self.imgsrc) except: img = Image(url=self.imgsrc) display(img) self.speak() alien_list.append(alien_interactiveV2('http://tinyurl.com/mnwj7wj','Ridley Scott','white','LV223','voluptuous')) interact(show_interactive, alien_number=(0,3)); # Define a child class that has alienV2 as parent class class alien_interactiveV3(alienV2): # Initialize child class function def __init__(self, *args,**kwargs): super(alien_interactiveV3, self).__init__(**kwargs) self.imgsrc = args[0] def show_yourself(self): try: img = Image(filename=self.imgsrc) except: img = Image(url=self.imgsrc) display(img) self.speak() characteristics = {'head':"like Homer Simpson's with tentacles", 'name':'Zoidberg', 'color':'pink', 'planet':' Decapod 10'} alien_list.append(alien_interactiveV3('http://tinyurl.com/ngdd6ul',**characteristics)) interact(show_interactive, alien_number=(0,4)); txt = 'my name is Zoidberg' txt txt.upper() import numpy as np np.__name__ np.__class__ np. class Integrator: def __init__(self, a, b, n): self.a, self.b, self.n = a, b, n self.points, self.weights = self.method() def method(self): raise NotImplementedError('no rule in class %s' %self.__class__.__name__)