Introduction to PythonCSCI 4448/5448: Object-Oriented Analysis & DesignLecture 81
Learning Objectives•Students will be able to…•Understand why Python is suited to OO designs•Find support for their own Python language exploration•Use Python for class projects and future designs2
Why Python?•Python: Development Speed•Python’s syntax emphasizes code readability – estimated that 10% of code required for a given application vs. C•Built-in memory management•Python has vast library support for UI and M2M/IoT communication protocols•Python’s interactive interpreter allows rapid test of features while programming•Python has a complete (?) implementation of object-oriented design elements•IEEE 2018 language survey places Python (#1) above C (#4) [1]•C++ and Java are #2 and #33
•When working in Python, and looking at examples, you will see the term “Pythonic”•Pythonic is code that is as simple and readable as possible•Pythonic code follows the basics of Python code style [4] •Consider using the pep8 tool to check your code for style issues [5]•More information on Python style is available [6]“Pythonic” Code Style4
•Beautiful is better than ugly.•Explicit is better than implicit.•Simple is better than complex.•Complex is better than complicated.•Flat is better than nested.•Sparse is better than dense.•Readability counts.•Special cases aren't special enough to break the rules.Although practicality beats purity.•Errors should never pass silently.•Unless explicitly silenced.Zen of Python: Tim Peters [7]5
•In the face of ambiguity, refuse the temptation to guess.•There should be one-- and preferably only one --obvious way to do it.•Although that way may not be obvious at first unless you're Dutch*.•Now is better than never.•Although never is often better than *right* now. •If the implementation is hard to explain, it's a bad idea.•If the implementation is easy to explain, it may be a good idea.•Namespaces are one honking great idea -- let's do more of those!•You can see the Zen of Python in a Python interpreter by entering:•>>> import this•*Dutch – refers to Guido van Rossum, Python’s creator, who is Dutch.Zen of Python: Tim Peters [7] continued…6
•Functional – Direct evaluation of mathematical functions (like R or Mathematica):>>> 1+23•Procedural – Structured programs based on modules and procedure calls (like C): def add_me(x,y):print(x+y)add_me(3,4)# Output: 7Python: Functional, Procedural…7
class MyMath:input_error = “Bad input”def error(self):print(self.input_error)def adder(x,y,self):print(x+y)m = MyMath()print(m.input_error)# Output: Bad inputprint(m.error())# Output: Bad inputprint(m.adder(3,4))# Output: 7Object-oriented – Data operations via objects with defined methods; inheritable classes (like Java and C++):Python: …and Object-Oriented8
Upload your study docs or become a
Course Hero member to access this document
Upload your study docs or become a
Course Hero member to access this document
End of preview. Want to read all 22 pages?
Upload your study docs or become a
Course Hero member to access this document