When Jose Lopex Portillo, president of Mexico, decided to bar the deposed shah of Iran from returning here last month, he did more than just anger Washington. He broke a long Mexican tradition of granting asylum to the politically persecuted.

Both famous and simple folk, covering the entire spectrum of political beliefs, have been welcome. After he was forced out of Russia by Stalin, Leon Trotsky lived his last years in exile here during the 1930s. Anti-Castro Cubans came here in the 1960s, only to be followed by left-wing Chileans in the 1970s.

Such is Mexico's adherence to this principle, that foreigners who were violent enemies at home suddenly find themselves face to face in the capital's coffee houses or university corridors.

During this year's Nicaraguan civil war, for example, the government here gave aid as well as asylum to the Sandinista guerillas who finally overthrew the Somoza regime. Now, as the government continues to pour financial and technical assistance into Sandista hands, it has also received one of their archenemies, Somoza's former National Police chief Nicolas Valle Salinas.

The apparent paradox does not seem to bother most Mexicans. It is part of the country's well-known pragmatism which rarely lets ideology get in the way.

Mexico's most famous foreign policy paradox dates back to the late 1960s when several thousand Spanish Republicans were given refuge here from Spain's civil war. Yet at the same time as the Republican families poured into the port of Veracruz, the warplanes of their enemy, Gen. Francisco Franco, were fueled with Mexican oil.

In the same month when the shah was refused entry here, two other deposed rulers arrived.

The first was former Argentine president Hector Campora, a moderate leftist who had spent almost four years in asylum in the Mexican Embassy in Buenos Aires waiting for permission to leave. Campora was let go after an official diagnosis that he suffered from cancer. He was immediately hospitalized in Mexico's Centro Medico, the hospital with extensive cancer treatment facilities sidestepped by the shah.

The second was Gen. Carlos Humberto Romero, who ran El Salvador's military government for two years until ousted by the October military coup.

After the coup, which was openly welcomed by Washington, Romero fled to neighboring Guatemala, where he had many friends. But according to knowledgeable sources he preferred to come to Mexico on a tourist visa and has now applied for residence papers here.

No one has criticized the arrival of Campora, the ailing former dentist who ruled Argentina for four months in 1973. But government officials here say that Romero's presence could provoke a violent attack from Salvadoran leftists who say that during Romero's repressive two-year regime more than 400 people were killed by security forces or disappeared.

No official figure for the number of exiles is readily available, but a guess by the Interior Ministry is that during the 1970s alone some 13,000 Latin Americans fled to Mexico.

The fact that most of the refugees are leftists does not point to Mexico's political preference but rather to the upsurgfe of rightist military regimes in this decade.

The list of refugees here in fact reads like a barometer of Latin America's political climate and it varies as political pressures change.

The hundreds of Nicaraguans who arrived here under the Somoza regime, for example, immediately left after the Sandinista victory in July.

Nearly 4,500 Chileans who fled after the 1973 coup against the Allende government are still living in Mexico, while almost as many Argentines and close to 900 Uruguayans also escaped here from military repression back home.

Despite Mexico's own very serious social problems and unemployment, many refugees have been given jobs, particularly the academics and journalists. m

But the exile scene is not without its difficulties.Mexicans historically have little personel liking for Latins from southern South America, whom they consider arrogant and opportunists. The South Americans fully reciprocate the antipathy, and often consider Mexicans to be mixed-blood inferiors.

Representatives of Swedish and Dutch foundations which provided large funds to create jobs for refugees, recently claimed that nearly half the projects designed for Argentines and Chileans have failed because they were "too lazy or too bourgeois to do the work."

One Dutch-sponsored project, for example, a new artichoke farm in Oaxaca, turned out to be run by Mexican peasants earning a miserable wage. "The four Chileans in charge had contracted the peasants while they moved back to Mexico City and took other jobs." the Dutch foundation representative said.