Presentation is loading. Please wait.

Presentation is loading. Please wait.

EN.540.635 - Software Carpentry Python – A Crash Course Function, Classes, and Graphing.

Similar presentations


Presentation on theme: "EN.540.635 - Software Carpentry Python – A Crash Course Function, Classes, and Graphing."— Presentation transcript:

1 EN.540.635 - Software Carpentry
Python – A Crash Course Function, Classes, and Graphing

2 What’s wrong here?

3 What’s wrong here?

4 What’s wrong here?

5 In General If some logic is repeated throughout your code, use a function! Why? With less repeated code, you can minimize copy-paste errors. Further, if the logic changes, you need only change one aspect of the code, not N aspects of it (where N is each copied instance) Exception: If the logic/action is repeated, but an alternative function already exists. That is, don’t re-invent the wheel! If some logic is only run once, then it makes no difference to have or not have a function, and can in fact be detrimental to reading the code if a function is made and displaced from said segment of code.

6 Object Oriented Programming (OOP)
An Idea From the 1950s to the 1970s, an idea was nurtured. Object Oriented Programming (OOP) Simply put, it was a method of programming in which objects could be defined that would hold data and methods. Data can be read as-is. That is, the object could hold variables that contain information. Methods can be read as “code”. That is, the object could hold functions that could then be run.

7 What are the basics of OOP?
Abstraction Polymorphism Encapsulation Inheritance

8 What are the basics of OOP?
Abstraction Think of a black box. Abstraction allows the user to only have to deal with the basics. Ex. The Python Imaging Library (PIL) allowed you to import an image just by running a = Image.load(“cat.jpeg”). How was the file read in? How did it know it was a .jpeg instead of a .png? Further, the object a now has methods such as a.getpixel((x, y))… how does it work?

9 What are the basics of OOP?
Polymorphism The idea that your code should be a “renaissance code” of sorts Think back to the Python Imaging Library (again…) When opening a file, all you called was a = Image.load(“another_cat.png”) Due to polymorphism, this method works for .jpeg, .png, and other file types!

10 What are the basics of OOP?
Encapsulation This is an idea that you can allow anyone to use your code, but you idiot-proof it first. Encapsulation means that the internal data and workings of your object are kept internal. No outsider can use it. In exchange, encapsulation requires you to make methods/functions that allows a user to access/change data they need. This is useful for many reasons, but I’ll list 2: In security contexts, you don’t want someone being able to read sensitive data. In regular use, your functions may assume data of a certain type is being passed, such as a float. By specifying a function such as a.set_x(4), you can force it to be a float, even when the user specifies a string.

11 What are the basics of OOP?
Inheritance Imagine you have an object that is 500 lines of code. Imagine you need 20 instances of this object with varying initial parameters, but all the same functions. Would you rather: Write the same 500 lines of code 20 times, changing the default values Initialize 20 objects from the default 500 lines of code, and change the values If you chose (b), you are thinking with inheritance and objects The reason (a) is bad is because if you need to change a single function, you need to do it 20 times now. Whereas in (b), you only need to do so once!

12 What are the basics of OOP?
Abstraction The logic is hidden, but it works. Polymorphism It works robustly too! Encapsulation It’s easy to move around, and hard to break Inheritance I can make many of them, and adapt off of them!

13 Example of OOP

14 Classes In Python, a class is how we can make an object
We will go over this together now in lecture Make a blank class Make variables and functions Initialize objects and mess around __init__, __str__ and __repr__, __del__, __lt__, __eq__, … __str__  for End Users, to be pretty output __repr__  for programmers, to be used for debugging purposes (as much info as possible)


Download ppt "EN.540.635 - Software Carpentry Python – A Crash Course Function, Classes, and Graphing."

Similar presentations


Ads by Google