import ex40_mystuff as mystuff try: print(mystuff.mystuff['apple']) # print(mystuff['apple]) # this is wrong!!! except TypeError as e: print(e) mystuff.apple() print(mystuff.pen) class MyStuff(object): # can remove (object), i.e. including () def __init__(self): self.pen = "I have an pen-n-apple" def apple(self): print("I am a fuji apple." + self.pen) thing = MyStuff() # instantiate print(thing.pen) thing.apple() class Song(object): def __init__(self, lyrics): # like C# constructor self.lyrics = lyrics def sing_me_a_song(self): for line in self.lyrics:
import ex40_mystuff ex40_mystuff.apple() print(ex40_mystuff.tangerine) class MyStuff(object): def __init__(self): self.tangerine = "And now a thousand years between" def apple(self): print("I AM CLASSY APPLES!") thing = MyStuff() thing.apple() print(thing.tangerine) class Song(object): def __init__(self, lyrics): self.lyrics = lyrics def sing_me_a_song(self): for line in self.lyrics: print(line) happy_bday = Song(["Happy birthday to you", "I don't want you to get sued", "So I'll stop right there"]) bulls_on_parade = Song(["They rally around tha family", "With pockets full of shells"])
import ex40_mystuff class MyStuff: def __init__(self): self.tangerine = "And now a thousand years between" def apple(self): print("I'm classy apples!") # dict style mystuff = {"apple": "I AM APPLES!"} print(mystuff["apple"]) # module style ex40_mystuff.apple() print(ex40_mystuff.tangerine) # class style thing = MyStuff() thing.apple() print(thing.tangerine)
import ex40_mystuff mystuff = {'apple': "I AM APPLES!"} print(mystuff['apple']) # dict style ex40_mystuff.apple() # module style # import ex40_mystuff print(ex40_mystuff.tangerine) # same thing, it's just variable class MyStuff(object): def __init__(self): self.tangerine = "And now a thousand years between" def apple(self): print("I AM CLASSY APPLES!") # class style thing = MyStuff() thing.apple() print(thing.tangerine) class Song(object): def __init__(self, lyrics): self.lyrics = lyrics def sing_me_a_song(self): for line in self.lyrics: print(line)
# importing a module i.e. a collection of functions and variab import ex40_mystuff # creating a dictionary and accesing a key-value pair mystuff = {'apple': 'I AM APPLES! [from the dictionary]'} print(mystuff['apple']) # accessing function from module with dot operator ex40_mystuff.apple() #accessing variable from module with dot operator print(ex40_mystuff.tangerine) print(mystuff['apple']) # get apple from the dictionary ex40_mystuff.apple() # get aple from the module print(ex40_mystuff.tangerine) # same thing, it's just a variableb # creating a class class MyStuff(object): def __init__(self): self.tangerine = "And now a thousand years between" def apple(self): print("I AM CLASSY APPLES!") thing = MyStuff() thing.apple() print(thing.tangerine)