SlideShare a Scribd company logo
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):
'''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.
 Continued
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)
Traceback (most recent call last):
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:
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.

More Related Content

PPSX
python Function
PDF
Wrapper classes
PDF
Alphorm.com Formation Big Data avec Apache Spark: Initiation
PPTX
Class, object and inheritance in python
PPT
Types of exceptions
PDF
An Abusive Relationship with AngularJS
PPTX
Installing and running Postfix within a docker container from the command line
PPTX
JUNit Presentation
python Function
Wrapper classes
Alphorm.com Formation Big Data avec Apache Spark: Initiation
Class, object and inheritance in python
Types of exceptions
An Abusive Relationship with AngularJS
Installing and running Postfix within a docker container from the command line
JUNit Presentation

What's hot (20)

PDF
Clean pragmatic architecture @ devflix
PDF
Static Analysis of Your OSS Project with Coverity
PDF
Mocking in Java with Mockito
PDF
JavaScript - Chapter 4 - Types and Statements
PPTX
Git 101 for Beginners
PDF
C++ 프로젝트에 단위 테스트 도입하기
PDF
PDF
Understanding Branching and Merging in Git
PDF
White Box Testing
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPTX
Vertical Slicing Architectures
PPT
Reliable Windows Heap Exploits
PPTX
JS Event Loop
PDF
Hands On with Maven
PPTX
Object Oriented Programming in Python
PPTX
String, string builder, string buffer
PDF
Learning git
PPTX
Git - Basic Crash Course
ODP
Introduction to BDD
PPTX
484478584-Presentation-on-Snake-game-pptx.pptx
Clean pragmatic architecture @ devflix
Static Analysis of Your OSS Project with Coverity
Mocking in Java with Mockito
JavaScript - Chapter 4 - Types and Statements
Git 101 for Beginners
C++ 프로젝트에 단위 테스트 도입하기
Understanding Branching and Merging in Git
White Box Testing
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Vertical Slicing Architectures
Reliable Windows Heap Exploits
JS Event Loop
Hands On with Maven
Object Oriented Programming in Python
String, string builder, string buffer
Learning git
Git - Basic Crash Course
Introduction to BDD
484478584-Presentation-on-Snake-game-pptx.pptx
Ad

Viewers also liked (13)

PPTX
Python: Modules and Packages
PPTX
Python: Basic Inheritance
PPTX
Python: Third-Party Libraries
PPTX
Python: The Iterator Pattern
PPTX
Python: Design Patterns
PPTX
Creating Objects in Python
PPTX
Python: Polymorphism
PPTX
Python: Manager Objects
PPTX
Introduction to Python programming
PPTX
Object-Orientated Design
PPTX
Python: Multiple Inheritance
PPTX
Python: Migrating from Procedural to Object-Oriented Programming
PPTX
The Extreme Programming (XP) Model
Python: Modules and Packages
Python: Basic Inheritance
Python: Third-Party Libraries
Python: The Iterator Pattern
Python: Design Patterns
Creating Objects in Python
Python: Polymorphism
Python: Manager Objects
Introduction to Python programming
Object-Orientated Design
Python: Multiple Inheritance
Python: Migrating from Procedural to Object-Oriented Programming
The Extreme Programming (XP) Model
Ad

Similar to Python: Access Control (20)

PPTX
object oriented porgramming using Java programming
PPTX
object oriented porgramming using Java programming
PPTX
Php oop (1)
PPTX
Lecture-10_PHP-OOP.pptx
PDF
Python lecture 8
PPTX
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
PPTX
Software enginnnering introduction (2).pptx
PDF
Object oriented approach in python programming
PPTX
Chapter 05 classes and objects
PPTX
Object oreinted php | OOPs
PPTX
My Presentation ITPdcjsdicjscisuchc.pptx
PPTX
My Presentation ITPsdhjccjh cjhj (1).pptx
PPTX
arthimetic operator,classes,objects,instant
PPTX
Introduction to oop
PPTX
Java interfaces
PPTX
Object Oriented Programming in Python.pptx
PDF
oopm 2.pdf
PPTX
Inheritance
PPTX
Object-oriented programming
PDF
Object-Oriented Programming System presentation
object oriented porgramming using Java programming
object oriented porgramming using Java programming
Php oop (1)
Lecture-10_PHP-OOP.pptx
Python lecture 8
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
Software enginnnering introduction (2).pptx
Object oriented approach in python programming
Chapter 05 classes and objects
Object oreinted php | OOPs
My Presentation ITPdcjsdicjscisuchc.pptx
My Presentation ITPsdhjccjh cjhj (1).pptx
arthimetic operator,classes,objects,instant
Introduction to oop
Java interfaces
Object Oriented Programming in Python.pptx
oopm 2.pdf
Inheritance
Object-oriented programming
Object-Oriented Programming System presentation

More from Damian T. Gordon (20)

PPTX
Introduction to Prompts and Prompt Engineering
PPTX
Introduction to Vibe Coding and Vibe Engineering
PPTX
TRIZ: Theory of Inventive Problem Solving
PPTX
Some Ethical Considerations of AI and GenAI
PPTX
Some Common Errors that Generative AI Produces
PPTX
The Use of Data and Datasets in Data Science
PPTX
A History of Different Versions of Microsoft Windows
PPTX
Writing an Abstract: A Question-based Approach
PPTX
Using GenAI for Universal Design for Learning
DOC
A CheckSheet for Inclusive Software Design
PPTX
A History of Versions of the Apple MacOS
PPTX
68 Ways that Data Science and AI can help address the UN Sustainability Goals
PPTX
Copyright and Creative Commons Considerations
PPTX
Exam Preparation: Some Ideas and Suggestions
PPTX
Studying and Notetaking: Some Suggestions
PPTX
The Growth Mindset: Explanations and Activities
PPTX
Hyperparameter Tuning in Neural Networks
PPTX
Early 20th Century Modern Art: Movements and Artists
PPTX
An Introduction to Generative Artificial Intelligence
PPTX
An Introduction to Green Computing with a fun quiz.
Introduction to Prompts and Prompt Engineering
Introduction to Vibe Coding and Vibe Engineering
TRIZ: Theory of Inventive Problem Solving
Some Ethical Considerations of AI and GenAI
Some Common Errors that Generative AI Produces
The Use of Data and Datasets in Data Science
A History of Different Versions of Microsoft Windows
Writing an Abstract: A Question-based Approach
Using GenAI for Universal Design for Learning
A CheckSheet for Inclusive Software Design
A History of Versions of the Apple MacOS
68 Ways that Data Science and AI can help address the UN Sustainability Goals
Copyright and Creative Commons Considerations
Exam Preparation: Some Ideas and Suggestions
Studying and Notetaking: Some Suggestions
The Growth Mindset: Explanations and Activities
Hyperparameter Tuning in Neural Networks
Early 20th Century Modern Art: Movements and Artists
An Introduction to Generative Artificial Intelligence
An Introduction to Green Computing with a fun quiz.

Recently uploaded (20)

PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Cell Types and Its function , kingdom of life
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Presentation on HIE in infants and its manifestations
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
RMMM.pdf make it easy to upload and study
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Cell Types and Its function , kingdom of life
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Presentation on HIE in infants and its manifestations
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
2.FourierTransform-ShortQuestionswithAnswers.pdf
A systematic review of self-coping strategies used by university students to ...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

Python: Access Control

  • 2. 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.
  • 3. 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.
  • 4. 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.
  • 5. 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 ( _ ).
  • 6. 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 ( _ ).
  • 7. 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:
  • 8. 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 
  • 9. 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.  Continued
  • 10. Access Control >>> secret_string = SecretString("ACME: Top Secret", “secretpass") >>> print(secret_string.decrypt("secretpass")) ACME: Top Secret
  • 11. 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) Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> print(secret_string.plain_string) AttributeError: 'SecretString' object has no attribute 'plain_string'
  • 12. Access Control >>> print(secret_string._SecretString_ _plain_string) ACME: Top Secret BUT:
  • 13. 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.
  • 14. etc.