简体   繁体   中英

Call a class attribute of a class instance from a function in python

Let's say I want to plot different class objects attribute from a function.

So far I have this:

...

import matplotlib.pyplot as plt

def plot2vars (deviceList, xVars, yVars, xLims, yLims, colormap=plt.cm.Spectral):
    x0 = xVars[0]
    x1 = xVars[1]
    y0 = yVars[0]
    y1 = yVars[1]
    fig, ax = plt.subplots(1,2)

    fig, ax = plt.subplots(1,2)
    for d in deviceList: #these 'd' are the class instances...
        if not d.discard:
                    ax[0].plot(d.x0, d.y0)
                    ax[0].set_xlim(xLims[0])
                    ax[0].set_ylim(yLims[0])

                    ax[1].plot(d.x1, d.y1)
                    ax[1].set_xlim(xLims[1])
                    ax[1].set_ylim(yLims[1])
    plt.show()

where deviceList is a list containing class instances with different attributes like, for example, u , z or T .

Now, when I call the function, I declare xVars, yVars, xLims, yLims as arrays of strings, which obviously doesn't work. But I don't know how to call these. And I don't even know how to look for this in the manuals...

plot2vars (
      deviceList, 
      xVars=['u', 'u'], yVars=['z', 'T'],  
      xLims=['', 'left=0.8'], yLims=['','bottom=0, top=0.8']
      )

Maybe, if you want to take the attributes given as strings from your xVars and yVars , you should use getattr method like this:

d.x0 -> getattr(d, x0)

For example, if x0 = 'qwerty' , getattr(d, x0) equals to d.qwerty .

So in your code you should use:

...
ax[0].plot(getattr(d, x0), getattr(d, y0))
...
ax[1].plot(getattr(d, x1), getattr(d, y1))
...

Documentation: https://docs.python.org/3/library/functions.html#getattr


As for xLims and yLims , I would define it as list of dictionaries like this:

xLims = [{}, {'left': 0.8}]
yLims = [{}, {'bottom': 0, 'top': 0.8}]

So this would allow me to use them through **kwargs approach:

...
ax[0].set_xlim(**xLims[0])
ax[0].set_ylim(**yLims[0])
...
ax[1].set_xlim(**xLims[1])
ax[1].set_ylim(**yLims[1])
...

The main idea is when you pass a dictionary to a function with ** the key-values pairs will be unpacked into key-value arguments.

So if I understand correctly, you are trying to access the attribute u of object d which would typically be called by writing du , but you want to be able to do that without defining ahead of time that the attribute in question is u .

d.x0 will look for an attribute of d which is called x0 , which has nothing to do the x0 you have defined.

The closest thing to what you're trying to do in this case is the getattr function: getattr(d, x0) should give you what you want.

That being said, it's not great practice if you can avoid using it. I would recommend simply passing du as argument to plot2vars and edit plot2vars accordingly when possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM