Skip to content

Latest commit

 

History

History
 
 

proxy

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Proxy Design Pattern

Videos

Section Video Links
Proxy Overview Proxy Overview Proxy Overview Proxy Overview
Proxy Use Case Proxy Use Case Proxy Use Case Proxy Use Case
__class__ Attribute __class__ Attribute __class__ Attribute __class__ Attribute
Circular Imports Circular Imports Circular Imports Circular Imports

Book

Cover Links
Design Patterns In Python (ASIN : B08XLJ8Z2J)    https://www.amazon.com/dp/B08XLJ8Z2J
   https://www.amazon.co.uk/dp/B08XLJ8Z2J
   https://www.amazon.in/dp/B08Z282SBC
   https://www.amazon.de/dp/B08XLJ8Z2J
   https://www.amazon.fr/dp/B08XLJ8Z2J
   https://www.amazon.es/dp/B08XLJ8Z2J
   https://www.amazon.it/dp/B08XLJ8Z2J
   https://www.amazon.co.jp/dp/B08XLJ8Z2J
   https://www.amazon.ca/dp/B08XLJ8Z2J
   https://www.amazon.com.au/dp/B08Z282SBC

Overview

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.

Terminology

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.

Proxy UML Diagram

Proxy Pattern UML Diagram

Output

python ./proxy/proxy_concept.py
1848118706080
pulling data from RealSubject
[1, 2, 3]
pulling data from Proxy cache
[1, 2, 3]

Example Use Case

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.

Example UML Diagram

Proxy Use Case Example

Output

python ./proxy/client.py
I am the form of a Lion
I am the form of a Leopard
I am the form of a Serpent
I am the form of a Leopard
I am the form of a Lion

New Coding Concepts

Changing An Objects Class At Runtime.

You change the class of an object by running self.__class__ = SomeOtherClass

Note that doing this does not affect any variables created during initialisation, eg self.variable_name = 'abc', since the object itself hasn't changed. Only its class methods and static attributes have been replaced with the class methods and static attributes of the other class.

This explains how calling tell_me_the_future() and tell_me_your_form() produced different results after changing self.__class__

Avoiding Circular Imports.

Normally in all the examples so far, I have been importing using the form

from module import Class

In /proxy/client.py I import the Lion module. The Lion module itself imports the Leopard and Serpent modules, which in turn also re import the Lion module again. This is a circular import and occurs in some situations when you separate your modules into individual files.

Circular imports will prevent the python interpreter from compiling your .py file into byte code.

The error will appear like,

cannot import name 'Lion' from partially initialized module 'lion' (most likely due to a circular import)

To avoid circular import errors, you can import modules using the form.

import module

and when the import is actually needed in some method

OBJECT = module.ClassName

See the Lion, Serpent and Leopard classes for examples.

Summary

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.