Wrapping homogeneous Python objects2019 Community Moderator ElectionPython Object WrapperCalling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?

Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?

How could an airship be repaired midflight?

Do I need to consider instance restrictions when showing a language is in P?

Is this an example of a Neapolitan chord?

Naive Monte Carlo, MCMC and their use in Bayesian Theory

Does multi-classing into Fighter give you heavy armor proficiency?

Constant Current LED Circuit

Differential and Linear trail propagation in Noekeon

Light propagating through a sound wave

Tikz: place node leftmost of two nodes of different widths

Recruiter wants very extensive technical details about all of my previous work

What can I do if I am asked to learn different programming languages very frequently?

Can you move over difficult terrain with only 5' of movement?

How can I wire 7 outdoor posts correctly?

Why Choose Less Effective Armour Types?

Is it correct to say "which country do you like the most?"

Have the tides ever turned twice on any open problem?

Violin - Can double stops be played when the strings are not next to each other?

Worshiping one God at a time?

World War I as a war of liberals against authoritarians?

Generic TVP tradeoffs?

My friend is being a hypocrite

How are passwords stolen from companies if they only store hashes?

Why is there so much iron?



Wrapping homogeneous Python objects



2019 Community Moderator ElectionPython Object WrapperCalling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?










6















I'm looking for a way to have a collection of homogeneous objects, wrap them in another object, but have the wrapper object have the same API as the original and forward the corresponding API call to its object members.



class OriginalApi:
def __init__(self):
self.a = 1
self.b = "bee"

def do_something(self, new_a, new_b, put_them_together=None):
self.a = new_a or self.a
self.b = new_b or self.b

if put_them_together is not None:
self.b = "".format(self.a, self.b)

# etc.

class WrappedApi:
def __init__(self):
self.example_1 = OriginalApi()
self.example_2 = OriginalApi()


Some possible solutions that have been considered, but are inadequate:




  • Rewriting the whole API Why not? Not adequate because the API is fairly large and expanding. Having to maintain the API in multiple spots is not realistic.



    Code example:



    class WrappedApi:
    def __init__(self):
    self.example_1 = OriginalApi()
    self.example_2 = OriginalApi()

    def do_something(self, new_a, new_b, put_them_together=None):
    self.example_1.do_something(new_a, new_b, put_them_together)
    self.example_2.do_something(new_a, new_b, put_them_together)



  • Using a list and a for-loop This changes the API on the object. That said, this is the backup solution in the event I can't find something more elegant. In this case, the WrappedApi class would not exist.



    Code example:



    wrapped_apis = [OriginalApi(), OriginalApi()]
    for wrapped_api in wrapped_apis:
    wrapped_api.do_something(1, 2, True)


  • I tried using
    Python Object Wrapper, but I could not see how to have it call multiple sub-objects with the same arguments.


And for anyone curious about the use case, it's actually a collection of several matplotlib axes objects. I don't want to reimplement to entire axes API (it's big), and I don't want to change all the code that makes calls on axes (like plot, step, etc.)










share|improve this question



















  • 2





    How do you want to handle the sequence protocol? Should indexing into the wrapper give you one of the wrapped items, or the result of indexing into all of the wrapped items? This is going to be complex in the generic case.

    – jonrsharpe
    3 hours ago












  • is this referring to def __getitem__(self, i):? If so, I hadn't thought about that, but I think either method would be valid for my situation

    – TinyTheBrontosaurus
    3 hours ago















6















I'm looking for a way to have a collection of homogeneous objects, wrap them in another object, but have the wrapper object have the same API as the original and forward the corresponding API call to its object members.



class OriginalApi:
def __init__(self):
self.a = 1
self.b = "bee"

def do_something(self, new_a, new_b, put_them_together=None):
self.a = new_a or self.a
self.b = new_b or self.b

if put_them_together is not None:
self.b = "".format(self.a, self.b)

# etc.

class WrappedApi:
def __init__(self):
self.example_1 = OriginalApi()
self.example_2 = OriginalApi()


Some possible solutions that have been considered, but are inadequate:




  • Rewriting the whole API Why not? Not adequate because the API is fairly large and expanding. Having to maintain the API in multiple spots is not realistic.



    Code example:



    class WrappedApi:
    def __init__(self):
    self.example_1 = OriginalApi()
    self.example_2 = OriginalApi()

    def do_something(self, new_a, new_b, put_them_together=None):
    self.example_1.do_something(new_a, new_b, put_them_together)
    self.example_2.do_something(new_a, new_b, put_them_together)



  • Using a list and a for-loop This changes the API on the object. That said, this is the backup solution in the event I can't find something more elegant. In this case, the WrappedApi class would not exist.



    Code example:



    wrapped_apis = [OriginalApi(), OriginalApi()]
    for wrapped_api in wrapped_apis:
    wrapped_api.do_something(1, 2, True)


  • I tried using
    Python Object Wrapper, but I could not see how to have it call multiple sub-objects with the same arguments.


And for anyone curious about the use case, it's actually a collection of several matplotlib axes objects. I don't want to reimplement to entire axes API (it's big), and I don't want to change all the code that makes calls on axes (like plot, step, etc.)










share|improve this question



















  • 2





    How do you want to handle the sequence protocol? Should indexing into the wrapper give you one of the wrapped items, or the result of indexing into all of the wrapped items? This is going to be complex in the generic case.

    – jonrsharpe
    3 hours ago












  • is this referring to def __getitem__(self, i):? If so, I hadn't thought about that, but I think either method would be valid for my situation

    – TinyTheBrontosaurus
    3 hours ago













6












6








6


2






I'm looking for a way to have a collection of homogeneous objects, wrap them in another object, but have the wrapper object have the same API as the original and forward the corresponding API call to its object members.



class OriginalApi:
def __init__(self):
self.a = 1
self.b = "bee"

def do_something(self, new_a, new_b, put_them_together=None):
self.a = new_a or self.a
self.b = new_b or self.b

if put_them_together is not None:
self.b = "".format(self.a, self.b)

# etc.

class WrappedApi:
def __init__(self):
self.example_1 = OriginalApi()
self.example_2 = OriginalApi()


Some possible solutions that have been considered, but are inadequate:




  • Rewriting the whole API Why not? Not adequate because the API is fairly large and expanding. Having to maintain the API in multiple spots is not realistic.



    Code example:



    class WrappedApi:
    def __init__(self):
    self.example_1 = OriginalApi()
    self.example_2 = OriginalApi()

    def do_something(self, new_a, new_b, put_them_together=None):
    self.example_1.do_something(new_a, new_b, put_them_together)
    self.example_2.do_something(new_a, new_b, put_them_together)



  • Using a list and a for-loop This changes the API on the object. That said, this is the backup solution in the event I can't find something more elegant. In this case, the WrappedApi class would not exist.



    Code example:



    wrapped_apis = [OriginalApi(), OriginalApi()]
    for wrapped_api in wrapped_apis:
    wrapped_api.do_something(1, 2, True)


  • I tried using
    Python Object Wrapper, but I could not see how to have it call multiple sub-objects with the same arguments.


And for anyone curious about the use case, it's actually a collection of several matplotlib axes objects. I don't want to reimplement to entire axes API (it's big), and I don't want to change all the code that makes calls on axes (like plot, step, etc.)










share|improve this question
















I'm looking for a way to have a collection of homogeneous objects, wrap them in another object, but have the wrapper object have the same API as the original and forward the corresponding API call to its object members.



class OriginalApi:
def __init__(self):
self.a = 1
self.b = "bee"

def do_something(self, new_a, new_b, put_them_together=None):
self.a = new_a or self.a
self.b = new_b or self.b

if put_them_together is not None:
self.b = "".format(self.a, self.b)

# etc.

class WrappedApi:
def __init__(self):
self.example_1 = OriginalApi()
self.example_2 = OriginalApi()


Some possible solutions that have been considered, but are inadequate:




  • Rewriting the whole API Why not? Not adequate because the API is fairly large and expanding. Having to maintain the API in multiple spots is not realistic.



    Code example:



    class WrappedApi:
    def __init__(self):
    self.example_1 = OriginalApi()
    self.example_2 = OriginalApi()

    def do_something(self, new_a, new_b, put_them_together=None):
    self.example_1.do_something(new_a, new_b, put_them_together)
    self.example_2.do_something(new_a, new_b, put_them_together)



  • Using a list and a for-loop This changes the API on the object. That said, this is the backup solution in the event I can't find something more elegant. In this case, the WrappedApi class would not exist.



    Code example:



    wrapped_apis = [OriginalApi(), OriginalApi()]
    for wrapped_api in wrapped_apis:
    wrapped_api.do_something(1, 2, True)


  • I tried using
    Python Object Wrapper, but I could not see how to have it call multiple sub-objects with the same arguments.


And for anyone curious about the use case, it's actually a collection of several matplotlib axes objects. I don't want to reimplement to entire axes API (it's big), and I don't want to change all the code that makes calls on axes (like plot, step, etc.)







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 5 mins ago









Peter Mortensen

13.8k1987113




13.8k1987113










asked 3 hours ago









TinyTheBrontosaurusTinyTheBrontosaurus

1,32511025




1,32511025







  • 2





    How do you want to handle the sequence protocol? Should indexing into the wrapper give you one of the wrapped items, or the result of indexing into all of the wrapped items? This is going to be complex in the generic case.

    – jonrsharpe
    3 hours ago












  • is this referring to def __getitem__(self, i):? If so, I hadn't thought about that, but I think either method would be valid for my situation

    – TinyTheBrontosaurus
    3 hours ago












  • 2





    How do you want to handle the sequence protocol? Should indexing into the wrapper give you one of the wrapped items, or the result of indexing into all of the wrapped items? This is going to be complex in the generic case.

    – jonrsharpe
    3 hours ago












  • is this referring to def __getitem__(self, i):? If so, I hadn't thought about that, but I think either method would be valid for my situation

    – TinyTheBrontosaurus
    3 hours ago







2




2





How do you want to handle the sequence protocol? Should indexing into the wrapper give you one of the wrapped items, or the result of indexing into all of the wrapped items? This is going to be complex in the generic case.

– jonrsharpe
3 hours ago






How do you want to handle the sequence protocol? Should indexing into the wrapper give you one of the wrapped items, or the result of indexing into all of the wrapped items? This is going to be complex in the generic case.

– jonrsharpe
3 hours ago














is this referring to def __getitem__(self, i):? If so, I hadn't thought about that, but I think either method would be valid for my situation

– TinyTheBrontosaurus
3 hours ago





is this referring to def __getitem__(self, i):? If so, I hadn't thought about that, but I think either method would be valid for my situation

– TinyTheBrontosaurus
3 hours ago












2 Answers
2






active

oldest

votes


















4














If you're only implementing methods then a generic __getattr__ can do the trick



class Wrapper: 
def __init__(self, x):
self.x = x
def __getattr__(self, name):
def f(*args, **kwargs):
for y in self.x:
getattr(y, name)(*args, **kwargs)
return f


For example with x = Wrapper([[], [], []]) after calling x.append(12) all the three list objects will have 12 as last element.



Note that the return value will always be None... an option could be collecting return values and returning them as a list but this of course would "break the API".






share|improve this answer

























  • This only works for methods and other callable attributes, though. Gathering the results into a list would be straightforward, but it'd be hard to distinguish between the cases where you have a list of Nones and should return the list, or a list of Nones and should return None.

    – jonrsharpe
    3 hours ago












  • @jonrsharpe: yes of course, but it's hard to multiplex a protocol that requires reading without changing the API. What should len(x) return if the contained objects don't answer the same?

    – 6502
    3 hours ago











  • Well, quite! As I said above, in the generic case this gets very complex.

    – jonrsharpe
    3 hours ago











  • good point on the return values. i'm hoping my usage of matplotlib gets away with ignoring most return values. And if it doesn't? Then... well... this get a lot harder. I'll ask a new question if that's the case.

    – TinyTheBrontosaurus
    1 hour ago


















1














I think you have the right idea here



wrapped_apis = [OriginalApi(), OriginalApi()]
for wrapped_api in wrapped_apis:
wrapped_api.do_something(1, 2, True)


You can define your wrapper class by inheriting from list and then handle the API calls to its items once it is created.



class WrapperClass(list):
def __init__(self, api_type):
self.api_type = api_type

for func in dir(api_type):
if callable(getattr(api_type, func)) and not func.startswith("__"):
setattr(self, func, lambda *args, **kwargs:
[getattr(o, func)(*args, **kwargs) for o in self])

w = WrapperClass(OriginalApi)
o1, o2 = [OriginalApi()]*2
w.append(o1)
w.append(o2)
print(w.do_something(1, 2, True))
# [None, None]
print(w[0].b)
# 12
print(w[1].b)
# 12
print(o1.b)
# 12


Here, I'm iterating every method in your API class and creating a method in the wrapper class that applies its arguments to all its list items. It then returns a list comprehension consisting of the results.



Needless to say, you should probably validate the type of a new object being appended to this WrapperClass like so,



def append(self, item):
if not isinstance(item, self.api_type):
raise TypeError('Wrong API type. Expected %s'.format(self.api_type))
super(WrapperClass, self).append(item)





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55211193%2fwrapping-homogeneous-python-objects%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    If you're only implementing methods then a generic __getattr__ can do the trick



    class Wrapper: 
    def __init__(self, x):
    self.x = x
    def __getattr__(self, name):
    def f(*args, **kwargs):
    for y in self.x:
    getattr(y, name)(*args, **kwargs)
    return f


    For example with x = Wrapper([[], [], []]) after calling x.append(12) all the three list objects will have 12 as last element.



    Note that the return value will always be None... an option could be collecting return values and returning them as a list but this of course would "break the API".






    share|improve this answer

























    • This only works for methods and other callable attributes, though. Gathering the results into a list would be straightforward, but it'd be hard to distinguish between the cases where you have a list of Nones and should return the list, or a list of Nones and should return None.

      – jonrsharpe
      3 hours ago












    • @jonrsharpe: yes of course, but it's hard to multiplex a protocol that requires reading without changing the API. What should len(x) return if the contained objects don't answer the same?

      – 6502
      3 hours ago











    • Well, quite! As I said above, in the generic case this gets very complex.

      – jonrsharpe
      3 hours ago











    • good point on the return values. i'm hoping my usage of matplotlib gets away with ignoring most return values. And if it doesn't? Then... well... this get a lot harder. I'll ask a new question if that's the case.

      – TinyTheBrontosaurus
      1 hour ago















    4














    If you're only implementing methods then a generic __getattr__ can do the trick



    class Wrapper: 
    def __init__(self, x):
    self.x = x
    def __getattr__(self, name):
    def f(*args, **kwargs):
    for y in self.x:
    getattr(y, name)(*args, **kwargs)
    return f


    For example with x = Wrapper([[], [], []]) after calling x.append(12) all the three list objects will have 12 as last element.



    Note that the return value will always be None... an option could be collecting return values and returning them as a list but this of course would "break the API".






    share|improve this answer

























    • This only works for methods and other callable attributes, though. Gathering the results into a list would be straightforward, but it'd be hard to distinguish between the cases where you have a list of Nones and should return the list, or a list of Nones and should return None.

      – jonrsharpe
      3 hours ago












    • @jonrsharpe: yes of course, but it's hard to multiplex a protocol that requires reading without changing the API. What should len(x) return if the contained objects don't answer the same?

      – 6502
      3 hours ago











    • Well, quite! As I said above, in the generic case this gets very complex.

      – jonrsharpe
      3 hours ago











    • good point on the return values. i'm hoping my usage of matplotlib gets away with ignoring most return values. And if it doesn't? Then... well... this get a lot harder. I'll ask a new question if that's the case.

      – TinyTheBrontosaurus
      1 hour ago













    4












    4








    4







    If you're only implementing methods then a generic __getattr__ can do the trick



    class Wrapper: 
    def __init__(self, x):
    self.x = x
    def __getattr__(self, name):
    def f(*args, **kwargs):
    for y in self.x:
    getattr(y, name)(*args, **kwargs)
    return f


    For example with x = Wrapper([[], [], []]) after calling x.append(12) all the three list objects will have 12 as last element.



    Note that the return value will always be None... an option could be collecting return values and returning them as a list but this of course would "break the API".






    share|improve this answer















    If you're only implementing methods then a generic __getattr__ can do the trick



    class Wrapper: 
    def __init__(self, x):
    self.x = x
    def __getattr__(self, name):
    def f(*args, **kwargs):
    for y in self.x:
    getattr(y, name)(*args, **kwargs)
    return f


    For example with x = Wrapper([[], [], []]) after calling x.append(12) all the three list objects will have 12 as last element.



    Note that the return value will always be None... an option could be collecting return values and returning them as a list but this of course would "break the API".







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 3 hours ago

























    answered 3 hours ago









    65026502

    87.3k13115217




    87.3k13115217












    • This only works for methods and other callable attributes, though. Gathering the results into a list would be straightforward, but it'd be hard to distinguish between the cases where you have a list of Nones and should return the list, or a list of Nones and should return None.

      – jonrsharpe
      3 hours ago












    • @jonrsharpe: yes of course, but it's hard to multiplex a protocol that requires reading without changing the API. What should len(x) return if the contained objects don't answer the same?

      – 6502
      3 hours ago











    • Well, quite! As I said above, in the generic case this gets very complex.

      – jonrsharpe
      3 hours ago











    • good point on the return values. i'm hoping my usage of matplotlib gets away with ignoring most return values. And if it doesn't? Then... well... this get a lot harder. I'll ask a new question if that's the case.

      – TinyTheBrontosaurus
      1 hour ago

















    • This only works for methods and other callable attributes, though. Gathering the results into a list would be straightforward, but it'd be hard to distinguish between the cases where you have a list of Nones and should return the list, or a list of Nones and should return None.

      – jonrsharpe
      3 hours ago












    • @jonrsharpe: yes of course, but it's hard to multiplex a protocol that requires reading without changing the API. What should len(x) return if the contained objects don't answer the same?

      – 6502
      3 hours ago











    • Well, quite! As I said above, in the generic case this gets very complex.

      – jonrsharpe
      3 hours ago











    • good point on the return values. i'm hoping my usage of matplotlib gets away with ignoring most return values. And if it doesn't? Then... well... this get a lot harder. I'll ask a new question if that's the case.

      – TinyTheBrontosaurus
      1 hour ago
















    This only works for methods and other callable attributes, though. Gathering the results into a list would be straightforward, but it'd be hard to distinguish between the cases where you have a list of Nones and should return the list, or a list of Nones and should return None.

    – jonrsharpe
    3 hours ago






    This only works for methods and other callable attributes, though. Gathering the results into a list would be straightforward, but it'd be hard to distinguish between the cases where you have a list of Nones and should return the list, or a list of Nones and should return None.

    – jonrsharpe
    3 hours ago














    @jonrsharpe: yes of course, but it's hard to multiplex a protocol that requires reading without changing the API. What should len(x) return if the contained objects don't answer the same?

    – 6502
    3 hours ago





    @jonrsharpe: yes of course, but it's hard to multiplex a protocol that requires reading without changing the API. What should len(x) return if the contained objects don't answer the same?

    – 6502
    3 hours ago













    Well, quite! As I said above, in the generic case this gets very complex.

    – jonrsharpe
    3 hours ago





    Well, quite! As I said above, in the generic case this gets very complex.

    – jonrsharpe
    3 hours ago













    good point on the return values. i'm hoping my usage of matplotlib gets away with ignoring most return values. And if it doesn't? Then... well... this get a lot harder. I'll ask a new question if that's the case.

    – TinyTheBrontosaurus
    1 hour ago





    good point on the return values. i'm hoping my usage of matplotlib gets away with ignoring most return values. And if it doesn't? Then... well... this get a lot harder. I'll ask a new question if that's the case.

    – TinyTheBrontosaurus
    1 hour ago













    1














    I think you have the right idea here



    wrapped_apis = [OriginalApi(), OriginalApi()]
    for wrapped_api in wrapped_apis:
    wrapped_api.do_something(1, 2, True)


    You can define your wrapper class by inheriting from list and then handle the API calls to its items once it is created.



    class WrapperClass(list):
    def __init__(self, api_type):
    self.api_type = api_type

    for func in dir(api_type):
    if callable(getattr(api_type, func)) and not func.startswith("__"):
    setattr(self, func, lambda *args, **kwargs:
    [getattr(o, func)(*args, **kwargs) for o in self])

    w = WrapperClass(OriginalApi)
    o1, o2 = [OriginalApi()]*2
    w.append(o1)
    w.append(o2)
    print(w.do_something(1, 2, True))
    # [None, None]
    print(w[0].b)
    # 12
    print(w[1].b)
    # 12
    print(o1.b)
    # 12


    Here, I'm iterating every method in your API class and creating a method in the wrapper class that applies its arguments to all its list items. It then returns a list comprehension consisting of the results.



    Needless to say, you should probably validate the type of a new object being appended to this WrapperClass like so,



    def append(self, item):
    if not isinstance(item, self.api_type):
    raise TypeError('Wrong API type. Expected %s'.format(self.api_type))
    super(WrapperClass, self).append(item)





    share|improve this answer





























      1














      I think you have the right idea here



      wrapped_apis = [OriginalApi(), OriginalApi()]
      for wrapped_api in wrapped_apis:
      wrapped_api.do_something(1, 2, True)


      You can define your wrapper class by inheriting from list and then handle the API calls to its items once it is created.



      class WrapperClass(list):
      def __init__(self, api_type):
      self.api_type = api_type

      for func in dir(api_type):
      if callable(getattr(api_type, func)) and not func.startswith("__"):
      setattr(self, func, lambda *args, **kwargs:
      [getattr(o, func)(*args, **kwargs) for o in self])

      w = WrapperClass(OriginalApi)
      o1, o2 = [OriginalApi()]*2
      w.append(o1)
      w.append(o2)
      print(w.do_something(1, 2, True))
      # [None, None]
      print(w[0].b)
      # 12
      print(w[1].b)
      # 12
      print(o1.b)
      # 12


      Here, I'm iterating every method in your API class and creating a method in the wrapper class that applies its arguments to all its list items. It then returns a list comprehension consisting of the results.



      Needless to say, you should probably validate the type of a new object being appended to this WrapperClass like so,



      def append(self, item):
      if not isinstance(item, self.api_type):
      raise TypeError('Wrong API type. Expected %s'.format(self.api_type))
      super(WrapperClass, self).append(item)





      share|improve this answer



























        1












        1








        1







        I think you have the right idea here



        wrapped_apis = [OriginalApi(), OriginalApi()]
        for wrapped_api in wrapped_apis:
        wrapped_api.do_something(1, 2, True)


        You can define your wrapper class by inheriting from list and then handle the API calls to its items once it is created.



        class WrapperClass(list):
        def __init__(self, api_type):
        self.api_type = api_type

        for func in dir(api_type):
        if callable(getattr(api_type, func)) and not func.startswith("__"):
        setattr(self, func, lambda *args, **kwargs:
        [getattr(o, func)(*args, **kwargs) for o in self])

        w = WrapperClass(OriginalApi)
        o1, o2 = [OriginalApi()]*2
        w.append(o1)
        w.append(o2)
        print(w.do_something(1, 2, True))
        # [None, None]
        print(w[0].b)
        # 12
        print(w[1].b)
        # 12
        print(o1.b)
        # 12


        Here, I'm iterating every method in your API class and creating a method in the wrapper class that applies its arguments to all its list items. It then returns a list comprehension consisting of the results.



        Needless to say, you should probably validate the type of a new object being appended to this WrapperClass like so,



        def append(self, item):
        if not isinstance(item, self.api_type):
        raise TypeError('Wrong API type. Expected %s'.format(self.api_type))
        super(WrapperClass, self).append(item)





        share|improve this answer















        I think you have the right idea here



        wrapped_apis = [OriginalApi(), OriginalApi()]
        for wrapped_api in wrapped_apis:
        wrapped_api.do_something(1, 2, True)


        You can define your wrapper class by inheriting from list and then handle the API calls to its items once it is created.



        class WrapperClass(list):
        def __init__(self, api_type):
        self.api_type = api_type

        for func in dir(api_type):
        if callable(getattr(api_type, func)) and not func.startswith("__"):
        setattr(self, func, lambda *args, **kwargs:
        [getattr(o, func)(*args, **kwargs) for o in self])

        w = WrapperClass(OriginalApi)
        o1, o2 = [OriginalApi()]*2
        w.append(o1)
        w.append(o2)
        print(w.do_something(1, 2, True))
        # [None, None]
        print(w[0].b)
        # 12
        print(w[1].b)
        # 12
        print(o1.b)
        # 12


        Here, I'm iterating every method in your API class and creating a method in the wrapper class that applies its arguments to all its list items. It then returns a list comprehension consisting of the results.



        Needless to say, you should probably validate the type of a new object being appended to this WrapperClass like so,



        def append(self, item):
        if not isinstance(item, self.api_type):
        raise TypeError('Wrong API type. Expected %s'.format(self.api_type))
        super(WrapperClass, self).append(item)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 hours ago

























        answered 2 hours ago









        darkskydarksky

        1,4261224




        1,4261224



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55211193%2fwrapping-homogeneous-python-objects%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

            Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

            Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar