Full Transcript

CSC 213 - Programming Language (2) DR OHUD ALMUTAIRI CSC 213 : PROGRAMMING LANGUAGE (2) 1 Course outlines  Topic 1: Object Oriented Programming  Topic 2: Exception Handling  Topic 3: Regular expressions  Topic 4: File Handling  Topic 5: Database  Topic 6: Multithreading...

CSC 213 - Programming Language (2) DR OHUD ALMUTAIRI CSC 213 : PROGRAMMING LANGUAGE (2) 1 Course outlines  Topic 1: Object Oriented Programming  Topic 2: Exception Handling  Topic 3: Regular expressions  Topic 4: File Handling  Topic 5: Database  Topic 6: Multithreading  Topic 7: GUI Programming Topic 8: Data Visualization in Python CSC 213 : PROGRAMMING LANGUAGE (2) 2 Course Info Software Tools ◦Python 3.11 ◦Python Interpreter and IDLE IDE. ◦Download: https://www.python.org/downloads/ CSC 213 : PROGRAMMING LANGUAGE (2) 3 Course Info Grading Midterm Exam (1) 20pt Midterm Exam (2) 20pt 60pt Quizzes, Assignments 10pt Project, lab exam 10pt Final exam 40pt Total points 100pt CSC 213 : PROGRAMMING LANGUAGE (2) 4 Contact Email: [email protected] MyU: @DrAlmutairi CSC 213 : PROGRAMMING LANGUAGE (2) 5 Object Oriented Programming Lecture 1 DR OHUD ALMUTAIRI CSC 213 : PROGRAMMING LANGUAGE (2) 6 Introduction to Object-Oriented Programming (OOP)  Object-Oriented Programming (OOP) is a programming paradigm that structures code around the concept of "objects."  These objects encapsulate data and behavior, making it easier to model and interact with real-world entities.  Object oriented programming is a way to think about “objects” in a program (such as variables, functions, etc)  A program becomes less a list of instruction and more a set of objects and how they interact.  Everything in Python is an object. CSC 213 : PROGRAMMING LANGUAGE (2) 7 Introduction to Object-Oriented Programming (OOP) OOP is based on four main principles: encapsulation, modularity, inheritance, and polymorphism.  Encapsulation: hiding design details to make the program clearer and more easily modified later.  Modularity: the ability to make objects stand alone so they can be reused (our modules). Like the math module.  Inheritance: create a new object by inheriting (like father to son) many object characteristics while creating or over-riding for this object.  Polymorphism: allow one message to be sent to any object and have it respond appropriately based on the type of object it is. CSC 213 : PROGRAMMING LANGUAGE (2) 8 OOP helps for software engineering  Software engineering (SE) is the discipline of managing code to ensure its long-term use.  takes existing code and modifies it.  makes the overall code simpler, easier to understand.  doesn't change the functionality, only the form! CSC 213 : PROGRAMMING LANGUAGE (2) 9 What is a Class?  A class is a template for creating objects in Python. It defines the structure and behavior of objects by specifying attributes and methods.  Classes allow you to create instances (objects) that share the same attributes and methods defined by the class. This concept enables reusability and organization of code.  Example: Imagine a "Student" class with attributes like name and age, and methods like "study" and "take_exam." Multiple student instances can be created based on this class. CSC 213 : PROGRAMMING LANGUAGE (2) 10 Class versus Instance Class: Defines attributes and methods that instances will have. Instance: Represents a specific object with its unique data.  Example: The analogy of the cookie cutter and a cookie. CSC 213 : PROGRAMMING LANGUAGE (2) 11 Class versus Instance  In OOP, You define a class as a way to generate new instances of that class.  Both the instances and the classes are themselves objects.  The structure of an instance starts out the same, as dictated by the class.  The instances respond to the messages defined as part of the class. CSC 213 : PROGRAMMING LANGUAGE (2) 12 Why a class  We make classes because we need more complicated, user-defined data types to construct instances we can use. Each class has potentially two aspects:  Attributes: Properties that store data related to the class. The data (types, number, names) that each instance might contain.  Methods: Functions defined within the class that perform actions. The messages that each instance can respond to. CSC 213 : PROGRAMMING LANGUAGE (2) 13 Anatomy of a Class  Class Name: The name used to define the class. The standard way to name a class in Python is called CapWords: Each word of a class begins with a Capital letter. no underlines. sometimes called CamelCase. makes recognizing a class easier.  Attributes: Properties that store data related to the class.  Methods: Functions defined within the class that perform actions.  Constructor (__init__ method): A special method that initializes attributes when an instance is created. CSC 213 : PROGRAMMING LANGUAGE (2) 14 The basic format of a class definition CSC 213 : PROGRAMMING LANGUAGE (2) 15 Creating a Class in Python constructure calling the attributes CSC 213 : PROGRAMMING LANGUAGE (2) 16 dir() function  The dir() function lists all the attributes of a class  You can think of these as keys in a dictionary stored in the class. CSC 213 : PROGRAMMING LANGUAGE (2) 17 pass keyword The pass keyword is used to signify that you have intentionally left some part of a definition (of a function, of a class) undefined ◦ by making the suite of a class undefined, we get only those things that Python defines for us automatically. CSC 213 : PROGRAMMING LANGUAGE (2) 18 CSC 213 : PROGRAMMING LANGUAGE (2) 19 Constructor  When a class is defined, a function is made with the same name as the class.  This function is called the constructor. By calling it, you can create an instance of the class.  Constructor (__init__ method): A special method that initializes attributes when an instance is created.  We can affect this creation (more later), but by default Python can make an instance. CSC 213 : PROGRAMMING LANGUAGE (2) 20 Dot reference  We can refer to the attributes of an object by doing a dot reference, of the form: object.attribute  The attribute can be a variable or a function.  It is part of the object, either directly or by that object being part of a class. CSC 213 : PROGRAMMING LANGUAGE (2) 21 dot reference  Examples, print(my_instance.my_val) Print a variable associated with the object my_instance my_instance.my_method() Call a method associated with the object my_instance.  Variable versus method, you can tell by the parenthesis at the end of the reference. CSC 213 : PROGRAMMING LANGUAGE (2) 22 An object-local value Once an object is made, the data is made by the same way as in any other Python situation, by assignment Any object can thus be augmented by adding a variable my_instance.attribute = 'hello'  New attribute shown in dir CSC 213 : PROGRAMMING LANGUAGE (2) 23 Class instance relationship Because each instance has its type of the class that it was made from, an instance remembers its class. This is often called the instance-of relationship Stored in the __class__ attribute of the instance CSC 213 : PROGRAMMING LANGUAGE (2) 24 didnt add attribute CSC 213 : PROGRAMMING LANGUAGE (2) 25 CSC 213 : PROGRAMMING LANGUAGE (2) 26 Scope  Scope refers to the region in a program where a variable can be accessed or modified.  The Object Scope Rule is an important aspect of variable access within classes and objects in Python. The Object Scope Rule involves three main components: 1. Class Attributes -> defined within the class itself. 2. Instance Attributes -> specific to each instance. 3. Methods -> functions defined within the class. CSC 213 : PROGRAMMING LANGUAGE (2) 27 CSC 213 : PROGRAMMING LANGUAGE (2) 28 CSC 213 : PROGRAMMING LANGUAGE (2) 29 Method CSC 213 : PROGRAMMING LANGUAGE (2) 30 Method versus Function A method and a function are closely related. They are both “small programs” that have parameters, perform some operation and (potentially) return a value.  The main difference is that methods are functions tied to a particular object. CSC 213 : PROGRAMMING LANGUAGE (2) 31 Difference in calling Function: method do_something(param1) Method: function method an_object.do_something(param1) This means that the object that the method is called on is always implicitly a parameter! CSC 213 : PROGRAMMING LANGUAGE (2) 32 Difference in definition Methods are defined inside the suite of a class. Methods always bind the first parameter in the definition to the object that called it. This parameter can be named anything, but traditionally it is named self class MyClass: def my_method(self,param1): suite CSC 213 : PROGRAMMING LANGUAGE (2) 33 more on self self is an important variable. In any method it is bound to the object that called the method. Through self, we can access the instance that called the method (and all of its attributes as a result) CSC 213 : PROGRAMMING LANGUAGE (2) 34 Binding self CSC 213 : PROGRAMMING LANGUAGE (2) 35 Binding self When a dot method call is made, the object that called the method is automatically assigned to self. We can use self to remember, and therefore refer, to the calling object to reference any part of the calling object, we must always precede it with self. The method can be written generically, dealing with calling objects through self CSC 213 : PROGRAMMING LANGUAGE (2) 36 End of the Lecture CSC 213 : PROGRAMMING LANGUAGE (2) 37

Use Quizgecko on...
Browser
Browser