Party switching by legislators has been common in many countries, including the Philippines, Italy, Nepal, Ecuador, Russia, and Japan. While frequently dismissed as simply an indicator of a weak parties, switching provides a unique window on party systems. To the extent that we understand affiliation decisions, we gain insight on the way politicians use parties to advance their careers. In this article I offer a model of party-membership patterns, where decisions to switch party or to stay put are a function of the strategic interaction of legislators and endogenous party leaders. I test the model on the case of Brazil, where switching is common. Results suggest that Brazilian legislators use parties to maximize pork, ideological consistency, and short-term electoral success, but which of these matters most depends on constituents, i.e., legislators use parties for different purposes in different electoral environments. The approach developed here could easily be applied to study legislative behavior in other political systems.