Classes in Python By Dustin Nguon Basics All
Classes in Python By Dustin Nguon
Basics �All members public �No private variables �Instead variables with two leading underscores are understood to be treated as private (ex. __count) �Member functions are virtual
Namespace �A mapping of names to objects �Namespaces include built-in names, global names, and local names. �Namespaces are separate from each other.
Scope �Textual area of namespace �Local -> Non-Local -> Global -> Built in �global declaration for variables you want in the global namespace �nonlocal declaration for variables in the local or non-local scope
Scope Example Def scope(): def local_assign(): var = “local” def nonlocal_assign(): nonlocal var = “nonlocal” def global_assign(); global var = “global var = “test” local_assing() print(“After local: ”, var) nonlocal_assign() print(“After nonlocal: ”, var) global_assing() print(“After global: ”, var) Scope() Print(“In global: ”, var) Output: After local: test After nonlocal: nonlocal After global: nonlocal In global: global
Syntax class Student: name = “Bob” def n(self) return ‘I go to Drexel’ def __init__(self, year): self. grade = year Program code
Class Objects �Instantiation �Var = Student(“Junior”) �__init__automatically called �Attributes can be called �var. name -> Bob var. grade -> Junior �var. name = “Bill”
Method Objects �Methods belonging to objects can be stored and then called later �Func = var. n �Print(Func()) -> I go to Drexel
Class and Instance Variables �From Student() �name is a class variable �name=“Bob” �Shared by all instances �grade is an instance variable �self. grade=year �Unique to each instance
Class Variables (cont. ) �Changing a class variable in one instance will put that change to all instances �Design classes so that attributes that should be unique are declared as instance variables �Ex. self. instancevar
Modify Class Variable Ex. a = Student(“Freshman”) b = Student(“Sophomore”) a. name = “Kyle” b. name = “Bill” a. Name -> Bill
Inheritence �Ex. class Child. Class(Parent. Class) �If parent is in a different module mod. parentclass �Child borrows attributes from parent �Child methods can override parent methods of the same name �You can also extend methods �Ex. Parentclass. method(self, params)
Inheritance Specific Methods �Isinstance(obj, type) �Returns true if the object is derived from the type �i. e. int �Issubclass(obj, type) �Returns true if the object is a subclass of the type
Multiple Inheritance �Ex. class Child. Class(Parent 1, Parent 2, …. . ) �Python searches each parent in argument order for attributes �Multiple paths upwards �Search order done dynamically �Any parent class is only hit once
Name Mangling �Python replaces “private” variables with _classname__privvar �Ex. rrivate variable __hair in Student() will be _Student__hair to python �Useful if you don’t want an overridden method to affect another method in the parent class
Struct(Python Version) Class Shape: pass Tri = Shape() Tri. numlines = 3 Tri. color = “blue”
Iterators �Calls __next__() until it reaches the element past the last one Ex. S = “ 12” Ita = iter(S) next(Ita) -> 1 next(Ita) -> 2 next(Ita) -> Stop. Iteration
Iterators and Classes �You can define how an iterators acts on a class �Ex. Iter(My. Class) �Simply define __iter__() and __next__() in the class �iter will return self �next will have the iteration implementation
Generators �Self made iterator �Uses yield to return data �Function next() knows where the generator left off Ex. def everyother(data): for index in range(0, len(data)-1, 2) yield data[index]
Exceptions �Also classes �Ex. class EX(Exception) �except listing a base class can pick up on derived classes �Opposite not true
- Slides: 20