# Manager to manage diamond pattern methods

**URL:** <https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845>\
**Category:** Python Help\
**Created:** [March 29, 2024, 5:07pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845 "2024-03-29T17:07:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Musa-Sina-Ertugrul](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/musa-sina-ertugrul/32/18881_2.png) [@Musa-Sina-Ertugrul](https://discuss.python.org/u/Musa-Sina-Ertugrul)\
**Post date:** [March 29, 2024, 5:07pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/1 "2024-03-29T17:07:03Z")

</div>

Hi, I developed easy to use way of using methods of diamond pattern methods. Would you like to add it on one of modules? If you want we or you can develop it. I am just trying to help Python community \<3

```python
from types import MethodType, FunctionType
from inspect import isclass
from functools import cache
from typing import Any,Self

class base_manager:

    def __init__ (self, target : object,
                 helper_class : type) -> None:
        if not isclass(helper_class):
            raise TypeError(f"{helper_class. __name__ } , this is not a class")
        self.__target: object = target
        self.__helper_class: type[Any] = helper_class

    
    def __enter__ (self) -> Self:
        return self()
    
    def __call__ (self) -> object:

        for obj_method in self.methods:
            setattr(self. __target,obj_method.__ name__,obj_method)
        return self.__target

    @property
    @cache
    def methods(self) -> list[MethodType]:
        
        return [MethodType(method, self.__target)for method in 
                            self.names_and_functions.values()]
    
    @property
    @cache
    def names_and_functions(self) -> dict:

        return {key:current_atr for key in 
                self. __target.__ class __.__ dict__
                if bool(current_atr := 
                            self. __helper_class.__ dict__.get(key,False)) 
                and key not in object. __dict__ and 
                callable(current_atr)}

    def __exit__ (self, exc_type, exc_val, exc_tb) -> None:

        for obj_method_key in self.names_and_functions:
            
            delattr(self.__target,obj_method_key)

```

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 29, 2024, 5:18pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/2 "2024-03-29T17:18:46Z")

</div>

Why did you feel this test was necessary?

> [@Musa-Sina-Ertugrul](#):
>
> `if not isinstance(target,object)`

---

<div class="post-metadata">

**Author:** ![Musa-Sina-Ertugrul](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/musa-sina-ertugrul/32/18881_2.png) [@Musa-Sina-Ertugrul](https://discuss.python.org/u/Musa-Sina-Ertugrul)\
**Post date:** [March 29, 2024, 5:20pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/3 "2024-03-29T17:20:42Z")

</div>

Yes, you are right sorry 🙏

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 29, 2024, 5:25pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/4 "2024-03-29T17:25:11Z")

</div>

No problem. And why on earth are you deleting attrs on the target in ` __exit__ `? That’s a tad, destructive. And posssibly even dangerous.

```python
    def __exit__ (self, exc_type, exc_val, exc_tb) -> None:

        for obj_method_key in self.names_and_functions:
            
            delattr(self.__target,obj_method_key)

```

Overall, what’s this code actually used for?

Perhaps this is just my lack of imagination and ignorance but “Diamond patterns” to my mind are a multiple inheritance pattern (I have not come across “diamond” in any other context in Python), and I can’t imagine how this code possibly helps that.

---

<div class="post-metadata">

**Author:** ![Musa-Sina-Ertugrul](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/musa-sina-ertugrul/32/18881_2.png) [@Musa-Sina-Ertugrul](https://discuss.python.org/u/Musa-Sina-Ertugrul)\
**Post date:** [March 29, 2024, 5:26pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/5 "2024-03-29T17:26:29Z")

</div>

```python
class GrandChild(ChildA, ChildB):
    def __init__ (self):
        super(). __init__ ()
    
    def print_name(self):
        super().print_name()
        print("pass")
        with base_manager(self,ChildB):
            self.print_name()

```

maybe like this

---

<div class="post-metadata">

**Author:** ![Musa-Sina-Ertugrul](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/musa-sina-ertugrul/32/18881_2.png) [@Musa-Sina-Ertugrul](https://discuss.python.org/u/Musa-Sina-Ertugrul)\
**Post date:** [March 29, 2024, 5:30pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/6 "2024-03-29T17:30:44Z")

</div>

I thought it more dynamic like more than 2

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [March 29, 2024, 5:39pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/7 "2024-03-29T17:39:20Z")

</div>

Could you please describe concertly what problem you are trying to solve? Generally, when proposing something to be added, it’s better to start with a use-case and/or problem statement instead of a solution. I have no clue when I would want to use this, and why.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 29, 2024, 5:40pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/8 "2024-03-29T17:40:06Z")

</div>

I don’t see the advantage over `ChildB.print_name(self)` or using `super`, but maybe someone else understands your intent better than me.

---

<div class="post-metadata">

**Author:** ![Musa-Sina-Ertugrul](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/musa-sina-ertugrul/32/18881_2.png) [@Musa-Sina-Ertugrul](https://discuss.python.org/u/Musa-Sina-Ertugrul)\
**Post date:** [March 29, 2024, 5:40pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/9 "2024-03-29T17:40:11Z")

</div>

Thanks, That is my first time sorry 🙏

---

<div class="post-metadata">

**Author:** ![Musa-Sina-Ertugrul](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/musa-sina-ertugrul/32/18881_2.png) [@Musa-Sina-Ertugrul](https://discuss.python.org/u/Musa-Sina-Ertugrul)\
**Post date:** [March 29, 2024, 5:45pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/10 "2024-03-29T17:45:24Z")

</div>

Yes, you are right. I have made over engineering ☹

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [March 30, 2024, 9:27pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/11 "2024-03-30T21:27:49Z")

</div>

That’s fine. You’ll come out of this with a deeper understanding than  
most people.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [March 30, 2024, 9:35pm UTC](https://discuss.python.org/t/manager-to-manage-diamond-pattern-methods/49845/12 "2024-03-30T21:35:20Z")

</div>

Yes. And no.

I’ve got a little context manager called `stackattrs` which I routinely  
use in setup then shutdown stuff like this:

```
 with stackattrs(
     obj,
     attr1=a,
     attr2=b,
 ):
     ... do stuff ...

```

which sets attrs on `obj` for the duration. Very useful in a number of  
situations. Anyway, it restores the previous state of those attributes  
on exit from the context. Particularly, if those attrs were not there at  
the start, it removes them. (Of course, if they were there previously  
with some value, that value gets restored.)

Now, I _do_ find myself going back to the init of `obj` sometimes and  
putting stub `self.attr1 = None` statements in sometimes. But often to  
make linters happy.

Having the attributes gone where you’re out of the context scope can be  
quite handy for discovering you’ve misused something because you get  
nice loud `AttributeError`s.

You can get `stackattrs` from my `cs.context` PyPI module if you want to  
play with it.
