Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is adding attributes dynamically frowned upon in Python?

In Python, you can assign an arbitrary attribute from outside the defining class:

class Profile(models.Model):
    user = models.OneToOneField(User)  
    name = models.CharField(max_length=140)

p = Profile()
p.age = 42

The underlying mechanism here is __dict__ attribute that maintains a dictionary of all attributes.

We were all told not to expose our inner workings to the client code, but attaching new data doesn't have to do with encapsulation at all, right? Is this idiom common for Python code?


Just What I Mean…

Each Tweet has standard fields, like id, text, owner.
When returning tweet list for a user, you want to display if a tweet is “favorited” by this user.

Obviously, to obtain is_favorite you need to query many-to-many relationship for this user.
Would it be OK to pre-fill Tweet objects with is_favorite corresponding to current user?

Sure I could expose a method is_favorite_for(user) but I'm hitting Django template language limitations that doesn't allow to call methods with arguments from inside the template. Also, I believe a template should not be calling methods at all.


I know this will work fine, but I wonder if doing something like that in an open source project would get other developers to look on me with contempt.

Sidenote:

I come from C#/.NET background where dynamic types were introduced very recently and aren't adapted widely except for some niche areas (interoperability, IoC frameworks, REST client frameworks, etc).

like image 928
Dan Abramov Avatar asked Oct 28 '11 16:10

Dan Abramov


2 Answers

My view is that it is a bad practice.

The object doesn't know that you're messing with its attributes. Consider, for example, what would happen if Profile were later expanded to have an attribute called age, unrelated to p.age in your code.

If you want to add attributes, why not subclass Profile, or have an external mapping of Profiles to an object with your custom attributes?

like image 80
NPE Avatar answered Sep 19 '22 23:09

NPE


I think the answer is: It depends. First, if you really want to prevent it you can by defining __slots__ in the class. And it is not generally a good practice to add attributes not actually defined in the class, as it can be confusing to someone reading the code and is rarely useful.

But at certain times, it is useful to be able to do this and Python documentation discusses this as a way to get something similar to a C struct or Pascal Record (see http://docs.python.org/tutorial/classes.html under section 9.7 Odds and Ends.)

like image 21
TimothyAWiseman Avatar answered Sep 22 '22 23:09

TimothyAWiseman