For example, a Prime Minister is voted by the party, so if they were to switch parties they would not be the leader of the other party. They have a function within their party.
But is the party of the US President anything more than a label?
Politics Stack Exchange is a question and answer site for people interested in governments, policies, and political processes. It only takes a minute to sign up.Sign up to join this community
Party in the USA is no more than a label for anyone.
Party affiliation is just a box you check when you register to vote. Its only real practical effect is that it allows you to vote in primary elections for the party you have selected*. Some states have restrictions on how often you can switch (to prevent partisans from purposely polluting the other party's primaries), but other than that, there are no restrictions on switching parties.
The parties don't have any say in who their constituents are either, or even over who runs as a candidate in their primaries. For instance, when I lived in Pennsylvania in the 90's, followers of Lyndon LaRouche used to show up on every primary ballot in my small Borough. They'd typically have very bland non-ethnic looking names, supposedly in hopes that low-information voters would pick them just based on name. My local party officials strenuously objected to this, but all they could do was post someone outside the official campaigning limit with sample ballots to hand out, and in particular pointing out which candidates were the LaRoucheites.
The point of this is that parties really have no control over who runs under their banner, or even their own membership.
* - For this reason, the only thing a person registering "independent" is really accomplishing in the USA is ensuring they have no say in who is running in the general election. However, even this isn't always the case. For example, the Democratic Party in Oklahoma currently allows independents (but not Republicans) to vote in its primaries. Minnesota (h/t to @SethR) doesn't even have official party membership.
Pretty much just a label. Obviously though if you switch your label it causes a big uproar with your constiutents. This hasn't ever happened with the president before, but it has happened with sitting senators. Most recently Senator Arlen Specter switched parties from Republican to a Democrat on April 28, 2009 (in the middle of his term). He stated that
As the Republican Party has moved farther and farther to the right, I have found myself increasingly at odds with the Republican philosophy and more in line with the philosophy of the Democratic Party.
He then switched his party and ran the next election as a Democrat (and lost).
Not quite the same as the President switching parties, but former President John Tyler was disowned by his party (the Whigs) in 1842. He did not switch parties, but instead was independent for the remainder of his term. From Wikipedia (emphasis mine):
On September 13, when the president did not resign or give in, the Whigs in Congress expelled Tyler from the party. Tyler was lambasted by Whig newspapers and received hundreds of letters threatening his assassination. Whigs in Congress were so angry with Tyler that they refused to allocate funds to fix the White House, which had fallen into disrepair.
In addition to the other examples mentioned in other answers, Teddy Roosevelt did switch to the Bull Moose party, but that was after he finished his term.Wiki link
More recently, for the 2016 presidential election, the Republican primary requested a "loyalty oath" consisting of the text:
“I, ________, affirm that if I do not win the 2016 Republican nomination for President of the United States I will endorse the 2016 Republican presidential nominee regardless of who it is.” ... “I further pledge that I will not seek to run as an independent or write-in candidate nor will I seek or accept the nomination for president of any other party.” (Source:)
I am not a lawyer, so I can't say how "run" is defined (as in does it just mean be in the running for election, or does it also included being the president afterwards), but there could be some arguments that this loath would preclude becoming independent or another party at least during the current term.
(Note that I picked on the 2016 Republican loyalty oath because it was the easiest to find (because at one point, questions on whether Trump would sign it was a hot topic) but I believe both major parties have had similar things in previous years as well (although a quick search suggests it being more common thing on the Republican side but I don't have a strong citation there))