How to create a touch switch to use with a Raspberry Pi
If you have low level access to the raspy GPIO's then there's an easy way to do that, all you need is a reisistor. Yeah, you read that right.

Just connect a pin to the lamp base and tie the resistor between the base and \$V_{cc}\$, that's the 3.3V rail, remember that the pins are not 5 volt tolerant.

Here is in pseudocode what you need to do software side:

while(1) {
    timer.reset()
    gpiox.direction = GPIO.OUT
    gpiox.write(GPIO.LOW)
    while(gpiox.read != GPIO.LOW)
    gpiox.direction = GPIO.IN
    timer.start()
    while(gpiox.read == GPIO.LOW)
    timer.stop()
    out = timer.read()
    if (out &gt; THRESHOLD)
        pressed = true
    else
        pressed = false
}


What's happening? First of all you reset a timer. It needs to be fast, something like a cpu timer that is incremented at each clock cycle or so.

You set your pin as ouput, write a zero to it and then wait for it to really go to zero. That may be done inside the write depending on the driver/HAL you are using, but for this thing to work you'll need to be using none. After the pin is really zero you set it as an input and start the timer. The parasitic capacitance of the lamp base and the pin input and, if present, your finger starts to charge through the resistance. When the voltage across the capacitance is read as a logic high you stop the timer: the trick is that if your finger is present the capacitance is bigger so it takes a lot of time more to load to the logic one, so you can tell the finger presence by reading the timer.

What problems might you encounter?
Well maybe you just don't have such a low level access to the raspy GPIO pins... But that's something you need to find out. Maybe you don't have such a low level timer. The biggest problem is that maybe the capacitance of the lamp base is very big so that touching it won't make a big difference.

This method anyway is worth a try because it costs a few cents. About the resistor, you want something that charges the capacitance slow enough for your timer to actually measure the time it takes, but fast enough to be actually able to sample the "button" maybe 20 times per second. Lamp plus body plus pin may be around 1nF, you want a \$\tau=RC\$ of about 20ms so $$R=\frac{\tau}{C}=\frac{20\cdot10^{-3}s}{1\cdot10^{-9}F}=20M\Omega$$
Well that's too much. You will need a fast timer and go for something around \$1M\Omega\$

But how does it work?
Everything around us has a capacitance with respect to something else. It's common to refer to ground as "something else", so everything has a capacitance with respect to ground. That said, let's consider the following circuit:







simulate this circuit &ndash; Schematic created using CircuitLab

\$C_{in}\$ is the digital port input capacitance plus the lamp base capacitance towards ground, usually around some 100pF, while \$C_H\$ (as in 'human') is the capacity that we have towards ground. When you touch the lamp base you close the switch labeled 'touch', while the raspberry can only act on \$SW_{in}\$. You start with \$SW_{in}\$ closed, so \$V_{C_{in}} = 0\$. When the raspberry opens \$SW_{in}\$ the input capacitance starts to charge to \$V_{cc}\$ through \$R_p\$ with a time costant \$\tau=R_p\cdot C_{in}\$. The V(t) law is quite known:
$$V_{C_{in}}(t) = V_{cc}(1-e^{-\frac{t}{\tau}})$$
The raspberry will detect a high input when voltage will reach something around \$\frac{2}{3}V_{cc}\$, so that will take:

$$t_{HIGH}=\tau\ln\Big(\frac{V_{cc}}{V_{cc}-\frac{2}{3}V_{cc}}\Big)=\tau\ln(3)$$

At this point it would stop the timer and check ho long did it take: if it's something around \$1.1\tau\$ no touch is occurring. The raspberry would close \$SW_{in}\$ and wait for the capacitor to discharge, i.e. waiting for the pin to read zero, to finally open the switch and start over.
But what if we close \$SW_h\$? Well, \$\tau\$ changes: \$\tau'=R_p\cdot (C_{in}+C_H)\$ now, and if \$C_{in}\$ is small enough, and hopefully it is, \$tau'\$ might even be ten times bigger than \$\tau\$. The raspberry will start its timer but now it will take considerably longer to read an high input:

$$t'_{HIGH}=...=\tau'\ln(3)\approx 10\cdot\tau\ln(3)=10\tau$$

The second to last relation is a sort of "we hope that's true" relation. When the raspberry will finally read an high input it will stop the timer and say well that take quite a lot of time, let's toggle the light for my programmer! And that's pretty much it.

And why does your solution work?
That's because your body is acting as an antenna. You are basically feeding the mains frequency in the raspberry input pin, and that can make the input read as one, but that would be an unreliable way to toggle your lamp. For what I can read your system is working quite randomly... Have a try at the proper way.