Access control Many languages have a notion of 'private' variables that can't be accessed from outside an object. For example, if we were sending someone.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Programming for Geographical Information Analysis: Core Skills Lecture 5: Working with others’ code I: Inheritance.
Solutions to Review Questions. 4.1 Define object, class and instance. The UML Glossary gives these definitions: Object: an instance of a class. Class:
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Creating Shareable Models By: Eric Hutton CSDMS - Community Surface Dynamics Modeling System (pronounced ˈ s ɪ stəms) Image by Flickr user Let There Be.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Guide to Programming with Python
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING DATA ABSTRACTION Jehan-François Pâris
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Object-Oriented Design Justin Callanan CS 597. Object-Oriented Basics ● Data is stored and processed in “objects” ● Objects represent generalized real.
Introduction to OOP OOP = Object-Oriented Programming OOP is very simple in python but also powerful What is an object? data structure, and functions (methods)
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Object-Oriented Programming Chapter Chapter
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Object Oriented Paradigm OOP’s. Problems with Structured Programming As programs grow ever larger and more complex, even the structured programming approach.
CSCE 240 – Intro to Software Engineering Lecture 3.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
OOP - Object Oriented Programming
OOP: Encapsulation &Abstraction
More Sophisticated Behavior
COMPSCI 107 Computer Science Fundamentals
Adapted from slides by Marty Stepp and Stuart Reges
Inheritance and Polymorphism
Adapted from slides by Marty Stepp and Stuart Reges
Classes.
Lecture 9-2: Interacting with the Superclass (super);
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Object Philosophy Object Orientation aspires to three philosophical approaches. Encapsulation: the notion that data and the procedures to work on them.
Style Generally style is built into Python, as it demands indents. However, good Python style is set out in PEP8:
Programming for Geographical Information Analysis: Core Skills
Object Oriented Programming in Python
To Get Started Paper sheet
Python Classes By Craig Pennell.
Recitation 6 Inheritance.
Object Oriented Programming (OOP) LAB # 8
More inheritance, Abstract Classes and Interfaces
Adapted from slides by Marty Stepp and Stuart Reges
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
CSc 110, Autumn 2016 Lecture 31: Encapsulation
Coding Concepts (Basics)
Adapted from slides by Marty Stepp and Stuart Reges
Java Programming, Second Edition
More About Inheritance & Interfaces
Lecture 10: Using Object-Oriented Languages
Inheritance.
Inheritance and Polymorphism
Migrating to Object-Oriented Programs
Topics OOP Review Inheritance Review Abstract Classes
Review We've seen that a module is a file that can contain classes as well as its own variables. We've seen that you need to import it to access the code,
CSG2H3 Object Oriented Programming
Presentation transcript:

Access control Many languages have a notion of 'private' variables that can't be accessed from outside an object. For example, if we were sending someone sensitive information, we might not want to give people direct access to it. Instead, we usually have 'public' accessor "get" and mutator "set" methods, which negotiate getting or changing private variables, for example checking the variable isn't in use or asking for a password. a = object.getVariableA() object.setVariableA(20) Indeed, accessor and mutator methods are generally regarded as good practice whatever the data: it's generally bad practice to access variables directly as you have no idea what else is using them.

Application Programming Interfaces Infact, it is usual to hide any variables and methods that outside code doesn't need access, limiting interactions to a clean and clear set of public methods. These public methods are the Application Programming Interface (API) of the code; the bits for connecting to it. These are often the bits described in the API documentation, often called the API or docs for short. This is part of Design by Contract: the idea that you design the public connections between code as the main program structure achieving a goal, allowing implementations within the code to be background detail.

Private In Python the security aspects of access control are less key, as the code is distributed as open text. Nevertheless, you can informally hide variables if there's no reason for outside code to interact with it. A function/method or variable name begun with an _underscore will informally be hidden when public descriptions of the code are made (for example, by dir(object) )

Private In addition, there is a chance that you want to make variables such that they can't be overridden by subclasses. If I make a variable in the sub and super classes, the subclass one will replace the superclass one, potentially causing issues if superclass methods are run. This is usually dealt with by making variables private, but in the absence of that option python engages in name mangling. Any label starting with at least two leading __underscores and at most one trailing underscore is replaced with _ClassName__name. This prevents subclasses accidentally overriding it. For more, see: https://docs.python.org/3/tutorial/classes.html#private-variables

Read only variables, get set Solution is to use the property() builtin function https://docs.python.org/3/library/functions.html#property This sets up a variable so it can only be accessed through accessor / mutator methods. When the variable is accessed as: object_name.variable_name What will run is: object_name.getvariable_name() This can be used to make variables read only, for example.

Example from docs class C(): def __init__(self): self._x = None def getx(self): return self._x def setx(self, value): self._x = value def delx(self): del self._x x = property(getx, setx, delx, "I'm the 'x' property.") https://docs.python.org/3/library/functions.html#property