Section | Video Links |
---|---|
Proxy Overview | |
Proxy Use Case | |
__class__ Attribute | |
Circular Imports |
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
python ./proxy/proxy_concept.py
1848118706080
pulling data from RealSubject
[1, 2, 3]
pulling data from Proxy cache
[1, 2, 3]
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.
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
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__
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.
... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.