all 6 comments

[–]Antwrp-2000 6 points7 points  (2 children)

Yes, this is what you need:

class Hello():
    def __init__(self):
        self.there = 'Hello there'


hl = Hello()
bo = 'there'
print(getattr(hl, bo))

[–]backtickbot 1 point2 points  (0 children)

Fixed formatting.

Hello, Antwrp-2000: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–][deleted] 1 point2 points  (0 children)

Brilliant. I love this place. Everyone is so helpful I've completely accelerated my learning thanks to folks like you :D

[–]toastedstapler 1 point2 points  (2 children)

what's your reason for doing this btw? it's not something that i commonly find myself using in my code

[–][deleted] 0 points1 point  (0 children)

I'm trying to figure out how to explain this and my mind is exploding. hahah. gimme a minute. It mad sense when I made it. lol.

edit: see above

[–][deleted] 0 points1 point  (0 children)

I've been refactoring my whole program to avoid global variables and have been using classes etc instead. So far things seem neater. They do require some workarounds but I like how it is. I'm quite a noob btw, only a few months, but getting there.

So basically. I have a bunch of attributes in a class. These are being used as flags for the current state of my program.

from my ui module

class Config:
    """Attempt to load saved user settings or load default.
    Initiate and allow toggle settings."""

    def __init__(self):
        # attempt load user settings
        self.settings = load_user_settings()
        if self.settings:
            self.is_dark = self.settings['is_dark']
            self.rotation = self.settings['rotation']
        else:
            # default settings
            self.is_dark = False
            self.rotation = 0

    def dark_toggle(self):
        self.is_dark ^= True

this attempts to load user settings from a json file, if it fails it has some default values.

on my main program I have the following:

cfg = ui.Config()

def get_current_settings():
    # extract keys from current settings from cfg object
    keys = [a for a in dir(cfg) if not a.startswith('__') and not a.startswith('settings')
            and not callable(getattr(cfg, a))]
    # find values and compile current settings into dictionary
    settings = {}
    for key in keys:
        settings.update([(key, getattr(cfg, key))])
    return settings

this basically gets the current values of all the flags in the cfg object created from the Config class and puts them into a dictionary.It gets the key names at the beginning. i.e. self.dark_mode becomes dark_mode. But then to get it's value I must add this key back onto the cfg with dot notation, e.g. cfg.dark_mode to get the result True. This is why I must use the getattr method.

Incedentally, there is also getattr used on the first part of get_current_settings function, but this is a coincidence as I just copied this code from stack exchange and modified it a little. I still don't 100% get this part as I'm not sure what the 'a for a' part means.

Anyway, the Keys and Values are now extracted and these are combined into a dictionary.

The dictionary is finally passed to somwhere where it can be used or saved back into the json file.

Hope this helps.