Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6252383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:47:31+00:00 2026-05-24T13:47:31+00:00

consider the following code: class MyClass(object): def __init__(self): self.data_a = np.array(range(100)) self.data_b = np.array(range(100,200))

  • 0

consider the following code:

class MyClass(object):

    def __init__(self):

        self.data_a = np.array(range(100))
        self.data_b = np.array(range(100,200))
        self.data_c = np.array(range(200,300))

    def _method_i_do_not_have_access_to(self, data, window, func):

        output = np.empty(np.size(data))

        for i in xrange(0, len(data)-window+1):
            output[i] = func(data[i:i+window])

        output[-window+1:] = np.nan

        return output

    def apply_a(self):

        a = self.data_a

        def _my_func(val):
            return sum(val)

        return self._method_i_do_not_have_access_to(a, 5, _my_func)

my_class = MyClass()
print my_class.apply_a()

The _method_i_do_not_have_access_to method takes a numpy array, a window parameter, and a user-defined function handle and returns an array containing values output by the function handle on window data points at a time of the input data array – a generic rolling method. I do not have access to changing this method.

As you can see, _method_i_do_not_have_access_to passes one input to the function handle which is the data array passed to _method_i_do_not_have_access_to. That function handle only computes output based window data points on the one data array passed to it through _method_i_do_not_have_access_to.

What I need to do is allow _my_func (the function handle passed to _method_i_do_not_have_access_to) to operate on data_b and data_c in addition to the array that is passed to _my_func through _method_i_do_not_have_access_to at the same window indexes. data_b and data_c are defined globally in the MyClass class.

The only way I have thought of doing this is including references to data_b and data_c within _my_func like this:

def _my_func(val):
    b = self.data_b
    c = self.data_c
    # do some calculations
    return sum(val)

However, I need to slice b and c at the same indexes as val (remember val is the length-window slice of the array that is passed through _method_i_do_not_have_access_to).

For example, if the loop within _method_i_do_not_have_access_to is currently operating on indexes 45 -> 50 on the input array, _my_func has to be operating on the same indexes on b and c.

The final result would be something like this:

def _my_func(val):

    b = self.data_b # somehow identify which slide we are at
    c = self.data_c # somehow identify which slide we are at

    # if _method_i_do_not_have_access_to is currently
    # operating on indexes 45->50, then the sum of 
    # val, b, and c should be the sum of the values at
    # index 45->50 at each

    return sum(val) * sum(b) + sum(c)

Any thoughts on how I might accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:47:32+00:00Added an answer on May 24, 2026 at 1:47 pm

    The question is how would _my_func know on which indizes to operate? If you know the indizes in advance when calling your function, the simplest approach would be just using a lambda: lambda val: self._my_func(self.a, self.b, index, val) with _my_func obviously changed to accommodate the additional parameters.

    Since you don’t know the indizes, you’ll have to write a wrapper around self.c that remembers which index was last accessed (or better yet catches the slice operator) and stores this in a variable for your function to use..

    Edit: Knocked up a small example, not especially great coding style and all, but should give you the idea:

    class Foo():
        def __init__(self, data1, data2):
            self.data1 = data1
            self.data2 = data2
            self.key = 0      
    
        def getData(self):
            return Foo.Wrapper(self, self.data2)
    
        def getKey(self):
            return self.key
    
        class Wrapper():
            def __init__(self, outer, data):
                self.outer = outer
                self.data = data
    
            def __getitem__(self, key):
                self.outer.key = key
                return self.data[key]
    
    if __name__ == '__main__':
        data1 = [10, 20, 30, 40]
        data2 = [100, 200, 300, 400]
        foo = Foo(data1, data2)
        wrapped_data2 = foo.getData()
        print(wrapped_data2[2:4])
        print(data1[foo.getKey()])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
Consider the following example code class A: def __init__(self, i): self.i = i print("Initializing
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following code: class myclass { function __construct(&$arg1, &$arg2) { echo $arg1; echo
Consider the following code: public class MyClass { public delegate string PrintHelloType(string greeting); public
Consider the following code: class MyClass { template <typename Datatype> friend MyClass& operator<<(MyClass& MyClassReference,
Consider the following code: abstract class X { def a:Unit a } class Y
Consider the following sample code: class MyClass { public long x; public void DoWork()
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following code: void Handler(object o, EventArgs e) { // I swear o

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.