1 / 16

CSE 231 Lab 11

Learn about creating and instantiating classes in Python, including attributes, methods, and the importance of the __init__ method. Understand the concept of self in classes and how to define and use member variables.

nancyharold
Télécharger la présentation

CSE 231 Lab 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 231 Lab 11

  2. Topics to Cover Today • Classes • Creating classes • Attributes and Methods in class • Instantiate class

  3. Classes • You’ve been working with a bunch of different classes all semester: list, set, dict, etc. • A class outlines how to “make” something and what that something “is.” • Classes are an Object Oriented Programming (OOP) paradigm.

  4. Classes

  5. Class and Instance D = set({1,2,3,4}) Creates an instance from set class set() is the constructor of the class set • Class is like a cookie cutter • Instance is like a cookie

  6. Classes – Creating a Class # here is the bare minimumyou need to make a class in Python class ClassName(): pass

  7. What is the purpose of this method? def __init__(self, A = 0, B = 1): self.__a = A self.__b = B • The moment you ask for an instance of a class to be created, the __init__ function is called. • So if you need to setup anything for an object, • it should go in init.

  8. Classes – What is __init__? Arguments you define for __init__ (besides self) are what you pass when you create an object. class SomeClass(): def __init__(self, name): self.name = name # instances have a member called name print(“HI ”, self.name) • >>> x = SomeClass() # ERROR • Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: __init__() missing 1 required positional argument: 'name'

  9. Classes – What is __init__? class SomeClass(): def __init__(self, name): self.name = name print(“Hi”, self.name) >>> x = SomeClass(‘Bob’) # good Hi Bob

  10. Classes – What is __init__? class SomeClass(): def __init__(self, name = ‘Bob’ ): self.name = name print(“Hi”, self.name) >>> x = SomeClass() • # good >>> x = SomeClass(‘ZACH’) • # good • Hi Bob • Hi ZACH

  11. What happens? class Time: def __init__(self, hour = 0, minute = 0, second = 0): print(hour,minute,second) What are the values of hour, minute and second if I create: C = Time(50,7) 50 7 0 C = Time(second = 83, hour = 24)) 24 0 83

  12. What is the purpose of this method? def __str__ (self): out_str = “{},{}”.format(self._a,self._b) return out_str

  13. Classes – Making a Method class SomeClass(): def __init__(self, number): self.number = number # member variable/attribute defget_square_number(self): return self.number ** 2 • >>> x = SomeClass(2) • >>> x. get_square_number() • 4

  14. Classes – What is self? class SomeClass(): def __init__(self, number): self.number = number defsquare(self): print(self.number**2) # <- self gets replaced with John! • >>> John = SomeClass(5) • >>> John. square() 25 • >>> Bob = SomeClass(6) • >>> Bob. square() 36

  15. Classes – What is self? class SomeClass(): def __init__(self, number): self.number = number defprint_number(self): print(number) • >>> Bob = SomeClass(6) • >>> Bob.print_number() NameError • # you forgot to mention self

  16. Classes – What is self? class SomeClass(): def __init__(self, number): self.number = number defprint_number(self): print(self.number) • >>> Bob = SomeClass(6) • >>> Bob.print_number() 6

More Related