Skip to content

Files

Latest commit

 

History

History
40 lines (26 loc) · 925 Bytes

protected-access.md

File metadata and controls

40 lines (26 loc) · 925 Bytes

Pattern: Accessing protected class member from outside

Issue: -

Description

This rule enforces object-oriented programming principle to disallow direct access to protected members. Use property to access the member or add protected member to __all__ to resolve this issue.

Example of incorrect code:

class Felinae(object):
    def __init__(self, genus):
        self._genus = genus

cat = Felinae("Puma")
# direct access of protected member
print "Name: %s".format(cat._genus)

Example of correct code:

class Felinae(object):
    def __init__(self, genus):
        self._genus = genus
        
    @property
    def genus(self):
        return self._genus        

cat = Felinae("Puma")
print "Name: %s".format(cat.genus)

Further Reading