Skip to content

Instantly share code, notes, and snippets.

@archmangler
Last active August 30, 2020 15:43
Show Gist options
  • Select an option

  • Save archmangler/e60d6dd5ca10a60eda65aba6fc091d6a to your computer and use it in GitHub Desktop.

Select an option

Save archmangler/e60d6dd5ca10a60eda65aba6fc091d6a to your computer and use it in GitHub Desktop.
Python 3 OOP Journey

Snippettes along the way to mastering OOP with Python

Progress

Book: Python3 Objected Oriented Programming 3rd Ed.

[] ch.3 - when objects are alike (p79 of 456)

  • In the programming world, duplicate code is considered evil. We should not have multiple copies of the same, or similar, code in different places.

[] ch.3 - when objects are alike (p83 of 456)

Basics

class Point:
    def __init__(self,x,y):
        self.move(x,y)

    def move(self,x=0,y=0):
        self.x = x
        self.y = y

    def reset(self):
        self.move(0,0)

    def calculate_distance(self,other_point):
        return math.sqrt( 
            ( self.x - other_point.x )**2 + 
            ( self.y - other_point.y )**2
        )

point1 = Point(3,5)
print(point1.x,point1.y)

point1.reset()

point1.move(3,4)

print(point1.x,point1.y)

print(point1.calculate_distance(point1))
The global keyword tells Python that the database variable inside initialize_database is the module level one we 
just defined. If we had not specified the variable as global, Python would have created a new local variable that
would be discarded when the method exits, leaving the module-level value unchanged. As these two examples 
illustrate, all module-level code is executed immediately at the time it is imported. However, if it is inside a 
method or function, the function will be created, but its internal code will not be executed until the function 
is called. This can be a tricky thing for scripts that perform execution (such as the main script in our e-commerce example). Sometimes, we write a program that does something useful, and then later find that we want to import a 
function or class from that module into a different program. However, as soon as we import it, any code at the module level is immediately executed. If we are not careful, we can end up running the first program when we really only 
meant to access a couple of functions inside that module.
  • and
To solve this, we should always put our start up code in a function (conventionally, called main) and 
only execute that function when we know we are running the module as a script, but not when our code 
is being imported from a different script. We can do this by guarding the call to main inside a conditional 
statement, demonstrated as follow
@archmangler

Copy link
Copy Markdown
Author

Python Access Control and OOP

Most object-oriented programming languages have a concept of access control. This is related to abstraction. Some attributes and methods on an object are marked private, meaning only that object can access them. Others are marked protected, meaning only that class and any subclasses have access. The rest are public, meaning any other object is allowed to access them.
Python doesn't do this. Python doesn't really believe in enforcing laws that might someday get in your way. Instead, it provides unenforced guidelines and best practices. Technically, all methods and attributes on a class are publicly available. If we want to suggest that a method should not be used publicly, we can put a note in docstrings indicating that the method is meant for internal use only (preferably, with an explanation of how the public- facing API works!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment