r/Python • python-programming.courses • Oct 30 '15

Improving your code readability with namedtuples

https://python-programming.courses/pythonic/improving-your-code-readability-with-namedtuples/
186 Upvotes

79 comments sorted by

View all comments

40

u/[deleted] Oct 30 '15

Fun style

from collections import namedtuple


class Person(namedtuple("_Person", ['name', 'age', 'height', 'weight'])):
    @property
    def bmi(self):
        return (self.weight / self.height) ** 2

    def at_bmi_risk(self):
        if self.age > 30 and self.bmi > 30:
            print("You're at risk")


michael = Person("Michael", age=40, height=1.8, weight=78)
michael.at_bmi_risk()

32

u/d4rch0n Pythonistamancer Oct 31 '15 edited Oct 31 '15

That's interesting, and I've definitely seen that pattern before. Great if you want immutable instances.

But, if you want the same low-memory instances with named attributes and functions and mutability, you can just define __slots__ = ('name', 'age', 'height', 'weight') under class Person.

It's not a dynamic dict anymore, you can't just do self.foo = 'bar' on an instance or you'll get an error, but it saves a shit ton of memory.

In python 2 and 3

https://docs.python.org/3.1/reference/datamodel.html#slots

Space is saved because dict is not created for each instance.

But if what you want is an immutable instance with functions, or just named attributes on data points, your pattern is awesome. Saves lots of memory too.

If you want a complete mindfuck, look at how they implement namedtuples. (ctrl-f ### namedtuple)

They use slots... but they also dynamically create the source code for the class and exec it. You can do some fun things with that, like generating high performance python source on the fly without a bunch of if statements and condition checks you know you won't need at runtime. And to anyone who says that's hacky, well shit they do it in the stdlib.

4

u/[deleted] Oct 31 '15

[removed] — view removed comment

3

u/alantrick Oct 31 '15

Well, at that point, it's not a tuple anymore, and you can just use dict or object

1

u/[deleted] Oct 31 '15

[removed] — view removed comment

1

u/ivosaurus pip'ing it up Nov 02 '15

I basically use it as a struct pattern. Most of the time the information I put in one doesn't change after creating it.

1

u/d4rch0n Pythonistamancer Oct 31 '15

One huge bonus is that you can use them as keys in a dictionary.

Another bonus is if you pass it to an external api, you know it's not going to be changed after the function returns.

If strings and ints were mutable, I can imagine there could be very strange consequences when you pass them as parameters into a third-party API.

→ More replies (2)

2

u/[deleted] Oct 31 '15

As an aside, mock is worth checking out for the same reason. wraps only gets you so far.

2

u/are595 Oct 31 '15

Wow, I never knew about __slots__. I just shaved 14% total run time off of a script I was writing that has to deal with a lot of objects (on the order of hundreds of thousands)! I didn't check memory usage, but I'm sure that went down as well.

Is there any place good for learning about these kinds of performance tips?

1

u/d4rch0n Pythonistamancer Oct 31 '15

Ha, nice catch! That's exactly the sort of case where it can help.

I haven't ran into any sites, but I think the best thing you could be doing is running cProfile if you don't already. Profiling your code is key. There's not much point to increase the performance of function foo if your code spends .1% of its time in there, and spends 20% of its time in bar. You can't know that without profiling (or some intense manual analysis).

Another thing you might look into is PyPy. People don't use that nearly as much as they could. Unless you use all the newest features of python 3.x, pypy is likely compatible with your code. If you have long-running scripts where the JIT can get warmed up, you can get huge performance increases sometimes. I had scripts that ran in the order of five minutes, and simply switching to pypy dropped it down to about half that. I experimented with some other date conversion thing and it dropped it to a quarter of the time.

And that is just a change in the environment, not the code base.

Here, just found this:

https://wiki.python.org/moin/PythonSpeed/PerformanceTips

I'll have to go through that a few times. Some great info there.

→ More replies (1)

6

u/jnovinger Oct 30 '15

Yes, I like this pattern a lot. I started playing with this concept to build light-weight Django model objects. As in, they take the same __init__ args and return something that looks and acts like a model. I abstracted out all the read-only methods I'd added to the model to a mixin class that was used both with this and the model.

Worked surprisingly well.

2

u/WittilyFun Oct 31 '15

This sounds really interesting, would you mind sharing a quick code snippet to help us (me) better understand?

5

u/squiffs Oct 31 '15

Why not just use a real class at this point?

3

u/elbiot Oct 31 '15

Whoa, really? No init?

Edit: I got it! Inheriting from a named tuple. Fascinating.

2

u/[deleted] Oct 31 '15

Even then, it wouldn't work, tuple and namedtuple require using __new__ to set instance attributes because they're immutable.

1

u/elbiot Oct 31 '15

What do you mean "wouldn't work"? I don't know what this comment is in referrence to.

1

u/[deleted] Oct 31 '15

Specifically the __init__. You can do stuff in the init, just not set values because they're set in place by tuple.__new__

1

u/elbiot Oct 31 '15

I'm comparing OP's named tuple solution to the common paradigm of doing it in init. Both of those definately work. I mean, good to know (what you said) but not really relevant.

1

u/[deleted] Oct 31 '15

Except it's completely relevant. You can't really use an __init__ when you inherit from tuple. I mean, you could but you can't set any instance variables, it's too late in the creation of the object at that point.

→ More replies (3)

2

u/d4rch0n Pythonistamancer Oct 31 '15

you know you don't need an __init__ function regardless right? I could see it being confusing since there's michael = Person("Michael", age=40, height=1.8, weight=78) but an __init__ isn't required regardless if you have a parent class with one or not.

1

u/elbiot Oct 31 '15

Usually you assign the values of attributes in the init. This skips having to do that. That is what was suprising to me.

1

u/d4rch0n Pythonistamancer Oct 31 '15

Oh yeah, definitely. As a quick hack before I wrote a class where it iterates on **kwargs and runs setattr(self, key, value) for each of them on the instance in its __init__.

Then I could write classes that inherit from it and you can remove a lot of boilerplate initialization. For smaller projects it works out.

1

u/elbiot Oct 31 '15

And this is way better than that because the class is explicit about it's attributes (user can't mess it up and create arbitrary attributes or leave out required ones). plus it's built in.