0

I'm trying do the follow sentence:

¬ = lambda x: not x

but I'm getting the follow error:

  File "<stdin>", line 1
    ¬ = lambda x: not x
    ^
SyntaxError: invalid character in identifier

Ok, I know that is an invalid sentence in Python, but I'm looking for a solution that allows me to use this syntax.

My target is create a script to run Propositional Logic Easily, and if I can do it, I'm be able to do some things with sugar syntax like as below:

¬ False
>>>True
ademar111190
  • 14,215
  • 14
  • 85
  • 114
  • 4
    Short answer: you can't. Long answer: Not in a portable way, edit the Python grammar and recompile, but don't expect your script to run anywhere else. – Martijn Pieters Dec 06 '14 at 17:57
  • 3
    Why are you trying to re-purpose Python to make a different language anyway? Why not just *parse the input* and then execute the result? You could use [sympy's propositional logic support](http://docs.sympy.org/latest/modules/logic.html) for the execution. – Martijn Pieters Dec 06 '14 at 17:59
  • I'm just studying @MartijnPieters , I do not want make another language or anything about it, Python is a powerful language and have a lot of good libraries and modules, like logic module that you have showed. If it have no way, Ok I understand, sorry for ask. – ademar111190 Dec 06 '14 at 18:10

2 Answers2

3

Short of rewriting the parser (which isn't a good idea), this is impossible in Python. Variable names may only contain letters, digits, and/or underscores. Additionally, the first character must be either a letter or an underscore. Also, a variable name cannot be the same as a keyword such as if or class.

Other than that, there can be no other characters in the name. You likewise cannot make your own keywords or operator symbols (although you can overload most of the existing operators on a custom class to do whatever you want. Here is a reference: operator overloading in python).

For more information, here is a link to Python's grammar specification and one which defines identifiers and keywords in Python.

Also, just for the record, you could use operator.not_ instead of making your own function to invert booleans.

Community
  • 1
  • 1
  • Thanks for answer, but you said "Variable names may only contain letters", Have no ways of put more characters like the exampled "¬" in the letters alphabet? After all an A and an ¬ are just a character. – ademar111190 Dec 06 '14 at 18:18
  • @ademar111190: yes, by *changing the parser*. Or by changing the Unicode standard as codified by Python. – Martijn Pieters Dec 06 '14 at 18:19
0

Thanks a lot for the help, the @iCodez has answered perfectly to python context, but in true I am opened to use others languages who can do it, and I found it on Ruby, below the ruby code that runs what I want:

#!/usr/local/bin/ruby
# coding: utf-8

σ = 9
def √(x)
    x**0.5
end
def ¬(x)
    not x
end

puts σ
puts √ σ
puts ¬ false

and the output:

9
3.0
true

thanks again for help! And thanks too to the programmers thread that help-me to find other languages.

Community
  • 1
  • 1
ademar111190
  • 14,215
  • 14
  • 85
  • 114