0

This is kind of a high level question. I'm not sure what you'd do with code like this:

class Object(object):
    pass

obj = Object
obj.a = lambda: None
obj.d = lambda: dict
setattr(obj.d, 'dictionary', {4,3,5})
setattr(obj.a, 'somefield', 'somevalue')

If I'm going to call obj.a.somefield, why would I use print? It feels redundant.

I simply can't see what programming strictly with setting attributes would be good for?

I could write an entire program with all of my variables in object classes.

Whitequill Riclo
  • 788
  • 2
  • 8
  • 19
  • 2
    The high level answer to "what you'd do with code like this" is "reject the PR". It's nonsensical and there's no reason to write code like this. – Samwise Oct 22 '22 at 16:44
  • 1
    that is my question. Why is this doable? what is the reason this is in python? – Whitequill Riclo Oct 22 '22 at 16:45
  • 1
    Attributes are a core part of Python objects. And what you show has its uses at times. But it doesn't mean you have to do things this way. – sj95126 Oct 22 '22 at 16:46
  • To return a value you use: `obj.d.dictionary` or `obj.a.somefield`. – Whitequill Riclo Oct 22 '22 at 16:47
  • @sj95126: what are ways that this is used and why is this not gone over in general python tutorials. I had to find this by just messing around. – Whitequill Riclo Oct 22 '22 at 16:49
  • people can find uses for a feature that exists, and while it's a very bad practice to use classes that way, there are situations where you just "wish you could overwrite a class attribute or method", such as when doing mocks/stubs for the purpose of testing. and while you normally don't find the above code in production, it's usually how testing frameworks work under the hood. – Ahmed AEK Oct 22 '22 at 17:20

1 Answers1

2

First about your print question. Print is used more for debugging or for attributes that are an output from an object that gives you information when you create it.

For example, there might be an object that you create by passing it data and it finds all of the basic statistics information of that data. You could have it return a dictionary via a method and access the values from there or you could simply access it via an attribute, making the data more readable.

For your second part of your question about why you would want to use attributes in general, they're more for internally passing information from function to function in an object or for configuring an object. Python has different scopes that determine which information each function can access. All methods of an object can access that object's attributes, which allows you to avoid using external or global variables. That makes your object nice and self contained. Global variables are generally avoided at all costs, because they can get messy, so they are considered bad practice.

Taking that a step further, using setattr is a more sophisticated way of setting these attributes to make your code more readable. You could use a function to modify aspects of an object or you could "hide" the complexity inside your setattr so the user can use a higher level interface rather than getting bogged down in the specifics.

  • I could be wrong but if I am reading the "Dive into Python 3" book, I'm pretty sure there is nothing about how using objects is best practices or how Global variables is avoided at all costs. Why are these "best practices" not important to teach to beginners? – Whitequill Riclo Oct 22 '22 at 17:01
  • When you're brand new to a language, you tend to use global variables in order to make your coding simpler. Once you get more advanced, you start to worry about how others will use your code. Bundling everything into a single object prevents issues with scope. When I say it "gets messy" I mean that when you access a global variable in a function, it can edit it. Unless you clearly mention it in your function description, users might not expect running a function to alter variables outside its scope. They would expect it to edit things in place that you pass it or to just return something – Acetyl-CodeA Oct 22 '22 at 17:12
  • I'm completely self-taught and I have never read anything about these best practices. – Whitequill Riclo Oct 22 '22 at 17:14
  • The key here is that you as a beginner are the only one looking at your programs so you know which functions are altering global variables and which aren't. You can't expect others using your code to know that without it being in your documentation or without looking into your code. Altering variables other than the ones passed to a function is atypical so you'll confuse people. – Acetyl-CodeA Oct 22 '22 at 17:15
  • See this article: https://stackoverflow.com/questions/19158339/why-are-global-variables-evil – Acetyl-CodeA Oct 22 '22 at 17:18