Access Control Damian Gordon.

Slides:



Advertisements
Similar presentations
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Advertisements

Classes 2 COMPSCI 105 S Principles of Computer Science.
Python classes: new and old. New and classic classes  With Python 2.2, classes and instances come in two flavors: old and new  New classes cleaned up.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Object-oriented Programming (review + a few new tidbits) "Python Programming for the Absolute Beginner, 3 rd ed." Chapters 8 & 9 Python 3.2 reference.
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.
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
Introduction to Pyrex September 2002 Brian Quinlan
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CSC 508 – Theory of Programming Languages, Spring, 2009 Week 3: Data Abstraction and Object Orientation.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
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)
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 17, 2015)
P YTHON ’ S C LASSES Ian Wynyard. I NTRODUCTION TO C LASSES A class is the scope in which code is executed A class contains objects and functions that.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
Object Oriented Programing (OOP)
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
Python: File Management Damian Gordon. File Management We’ve seen a range of variable types: – Integer Variables – Real Variables – Character Variables.
Chapter 1 Object Orientation: Objects and Classes.
Objects in Python: Part 2 Damian Gordon. Initialising an Object.
Object-Orientated Design (Summary)
CSC 108H: Introduction to Computer Programming
Introducing Python Introduction to Python.
Reference: Object Oriented Design and Programming (Horstmann)
Introduction to Computing Science and Programming I
More on Functions (Part 2)
Python 3 Some material adapted from Upenn cis391 slides and other sources.
COMPSCI 107 Computer Science Fundamentals
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Example: Vehicles What attributes do objects of Sedan have?
Modules and Packages Damian Gordon.
Introduction to Python
CS-104 Final Exam Review Victor Norman.
Copyright (c) 2017 by Dr. E. Horvath
Fundamentals of Programming II Interfaces and Implementations
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Functions CIS 40 – Introduction to Programming in Python
Lecture VI Objects The OOP Concept Defining Classes Methods
Engineering Computing
Object-Oriented Programming
Creating and Deleting Instances Access to Attributes and Methods
Object Oriented Programming in Python Session -3
Python Classes By Craig Pennell.
Guide to Programming with Python
Object Oriented Programming in Python
Basic Inheritance Damian Gordon.
More on Functions (Part 2)
Python programming exercise
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Programming in JavaScript
Ruby Classes.
Programming in JavaScript
CSC1018F: Object Orientation, Exceptions and File Handling (Tutorial)
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Python classes: new and old
Lecture 18 Python OOP.
Python classes: new and old
Migrating to Object-Oriented Programs
Introduction to Dictionaries
Functions and Abstraction
Presentation transcript:

Access Control Damian Gordon

Access Control In most other object-orientated programming languages, methods and attributes of an object can be classified as: PRIVATE: Meaning only the object can access it. PROTECTED: Meaning only that class and sub-classes can access it. PUBLIC: Meaning any other object can access it.

Access Control In most other object-orientated programming languages, methods and attributes of an object can be classified as: PRIVATE: Meaning only the object can access it. PROTECTED: Meaning only that class and sub-classes can access it. PUBLIC: Meaning any other object can access it.

Access Control Python doesn’t work like that! If want to make a method or attribute private, the easiest way of doing it is to add a comment in the docstrings at the start of the class indicating which methods or attributes are for internal use only.

Access Control Nonetheless, by convention, if we prefix a method or attribute with a single underscore ( _ ) we are telling everyone that this method or attribute should not be used by other objects except in the most dire circumstances. But there is nothing in the interpreter to stop external objects from access a method or attribute with an underscore ( _ ).

Access Control Nonetheless, by convention, if we prefix a method or attribute with a single underscore ( _ ) we are telling everyone that this method or attribute should not be used by other objects except in the most dire circumstances. But there is nothing in the interpreter to stop external objects from access a method or attribute with an underscore ( _ ).

Access Control If we want to more strongly suggest that the methods or attributes are for internal use only we can use a double-underscore ( _ _ ) which means Python will try a little harder to make it private, but still… Let’s look at an example:

Access Control class SecretString: ‘‘‘A not-at-all secure way to store a secret string.’’’ def __init__(self, plain_string, pass_phrase): self.__plain_string = plain_string self.__pass_phrase = pass_phrase # End __init__ Continued 

Access Control def decrypt(self, pass_phrase):  Continued Access Control def decrypt(self, pass_phrase): '''Only show the string if the pass_phrase is correct.''' if pass_phrase == self.__pass_phrase: # THEN return self.__plain_string else: return '' # Endif; # End decrypt. # End Class.

Access Control >>> secret_string = SecretString("ACME: Top Secret", “secretpass") >>> print(secret_string.decrypt("secretpass")) ACME: Top Secret

Access Control >>> print(secret_string.__plain_string) Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> print(secret_string.__plain_string) AttributeError: 'SecretString' object has no attribute '__plain_string‘ >>> print(secret_string.plain_string) File "<pyshell#10>", line 1, in <module> print(secret_string.plain_string) AttributeError: 'SecretString' object has no attribute 'plain_string'

Access Control >>> print(secret_string._SecretString_ _plain_string) ACME: Top Secret BUT:

secret_string._SecretString_ _plain_string Access Control OK, so the double underscore won’t 100% guarantee that a method or attribute can’t be accessed by another class, but it does make it more awkward: secret_string._SecretString_ _plain_string And should be sufficient to discourage other programmers for using objects you wish to keep internal.

etc.