(La)TeX "variables" are either macros or registers. The registers can be counters (integers), dimensions, skips (both fractional numbers with units) or read/write handlers etc. Note that you can change them inside a group, i.e. either { .. } or \begingroup ... \endgroup or a LaTeX environment, then their old value is restored at the end of the group automatically.
The integer settings like \hyphenpenalty are basically special count registers. You can copy their current value into any other count register and copy it back afterwards, using \somecount=\someothercount, where the = is optional. (Note that count is a TeX type, the LaTeX counters are based on them)
LaTeX provides two temporary count registers called \@tempcnta and \@tempcntb. In general it is a good idea to only change them in a local group, but if you can add one you don't need to temporary store the value anyway. Some other code used by you might change such general temporary registers, so sometimes it might be required to use an own count register using \newcount\yourtempcount.
% In the preamble, only once:
\newcount\yourtempcount
% Save and restore:
\yourtempcount=\hyphenpenalty
\hyphenpenalty=10000
\@starttoc{toc}%
\hyphenpenalty=yourtempcount
It is also possible to do it without a temporary register, by putting the restore code into a (usually temporary) macro. Here the current value is expanded using \the:
% Save and restore:
\edef\myrestorecode{\hyphenpenalty=\the\hyphenpenalty\relax}
\hyphenpenalty=10000%
\@starttoc{toc}%
\myrestorecode
This defines \myrestorecode to \hyphenpenalty=<current value, e.g. "50">\relax, because \hyphenpenalty on its own is not expandable. The \relax is added as an endmarker for the number. A single space (\space) would also be find. The benefit here is that you don't need a register (which requires to be declared first) and that this also works with multiple values:
\edef\myrestorecode{%
\hyphenpenalty=\the\hyphenpenalty\relax
\someothercountordim=\the\someothercountordim\relax
% ...
}
It works both count and dimension/skip registers.
\someregister=10000you don't need and should not add a%. The newline acts as space and a TeX will take a space as terminator for the number and remove it afterwards. With the%TeX actually looks at the beginning of the next line for further digits of the number and might even expand tokens/macros to find them until it finds something which isn't expandable or no digit. So having a space/linebreak or\relaxafter the number is actually a good thing. – Martin Scharrer Apr 15 '12 at 12:09