$f$ is continuous on $X$ iff $f$ is continuous on every compact subset

Let $(X,d)$ be a metric space,then prove that $f$ is continuous on $X$ iff $f$ is continuous on every compact subset of $X$.

If $f$ is continuous on $X$ then its restriction on each compact subset will be continuous, and conversely if $f$ is continuous on each compact set then for $x\in X$ the set $\{x\}$ is compact and thus $f$ must be continuous at $x$ , as $x$ was arbitrary $f$ will be continuous on $X$.


Solution 1:

Hint: $f$ is continuous if for every convergent sequence $x_n \to x$ in $X$, $f(x_n) \to f(x)$. Consider the set $$ \{x_n : n \in \Bbb N\} \cup \{\lim_{n \to \infty} x_n \} \subset X $$