Python Forum
if i had written my own language
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if i had written my own language
#1
if i had written my own language there are some things i would have done differently.

Output:
lt1/forums /home/forums 2> py2 Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> bool('True') True >>> bool('False') True >>> lt1/forums /home/forums 3> py3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> bool('True') True >>> bool('False') True >>> lt1/forums /home/forums 4>
can you guess what one of them is?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Output:
>>> bool('True') True >>> bool('') False >>> bool('False') True >>> bool('false') True >>> bool('not True') True >>> bool('7 < 5') True >>> bool('This sentence is false.') True >>> bool('vim and emacs are both good text editors.') True
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Then your language would be weird

Output:
>>> bool('') False >>> bool('a string or rather any container will result to false if empty') True >>> bool('it doesnt matter what the content is, its always true') True >>> bool([]) False >>> bool(['element']) True >>> bool({}) False >>> bool({'key':'value'}) True >>> bool(()) False >>> bool(('element',)) True
Recommended Tutorials:
Reply
#4
Non-empty object is always True.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Apr-02-2017, 01:49 PM)wavic Wrote: Non-empty object is always True.

Not exactly, its considered True unless implements __bool__ or __len__ (in that case __bool__ or __len__ is used to decide).

Not practical example:
>>> class A(list):
...   def __bool__(self): return False
... 
>>> a = A([1,2,3])
>>> print(len(a), a, bool(a))
3 [1, 2, 3] False
Reply
#6
(Apr-02-2017, 03:13 AM)Skaperen Wrote: if i had written my own language there are some things i would have done differently.
can you guess what one of them is?

Let me guess, a string containing the word "false" would be evaluated to the boolean value False?

How about adding strings containing the words "two" and "three"? Will the result be "five" then?
Reply
#7
(Apr-02-2017, 04:28 PM)Kebap Wrote:
(Apr-02-2017, 03:13 AM)Skaperen Wrote: if i had written my own language there are some things i would have done differently.
can you guess what one of them is?

Let me guess, a string containing the word "false" would be evaluated to the boolean value False?

How about adding strings containing the words "two" and "three"? Will the result be "five" then?

i want a funny button in addition to a like button.

but yeah, i would have valid code expression of any value of the type/class return that value.  so if the language accept "False" but not "false" then the function of that type will, as well.  but i don't really see a problem with a special set of functions to do that.  this could probably be done with the help of exec().  i might try that.

one feature my language would have is a way to add/extend/replace methods of standard type classes like bool, int, float, str, etc.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Apr-03-2017, 01:55 AM)Skaperen Wrote: this could probably be done with the help of exec()
There is a safe alternative for this use case
Output:
>>> from ast import literal_eval >>> help(literal_eval) Help on function literal_eval in module ast: literal_eval(node_or_string) Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. >>> literal_eval("True") True >>> literal_eval("False") False >>> literal_eval("false") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 80, in literal_eval return _convert(node_or_string) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 79, in _convert raise ValueError('malformed string') ValueError: malformed string
Of course, exec / eval can be made safe with scoping, but it still seems heavy-handed here given ast.literal_eval.
Reply
#9
(Apr-02-2017, 02:41 PM)zivoni Wrote:
(Apr-02-2017, 01:49 PM)wavic Wrote: Non-empty object is always True.

Not exactly, its considered True unless implements __bool__ or __len__ (in that case __bool__ or __len__ is used to decide).

Not practical example:
>>> class A(list):
...   def __bool__(self): return False
... 
>>> a = A([1,2,3])
>>> print(len(a), a, bool(a))
3 [1, 2, 3] False

You cheat  Tongue
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(Apr-03-2017, 01:55 AM)Skaperen Wrote: one feature my language would have is a way to add/extend/replace methods of standard type classes like bool, int, float, str, etc.

To some extent you can do it in python (unless you mean directly replacing str with your new str). 

class sstr(str):
    _nums = ['zero', 'one', 'two', 'three', 'four', 'five']
    def __add__(self, other):        
        try:
            sel, oth = self.strip().lower(), other.strip().lower()
            return sstr(self._nums[self._nums.index(sel) + self._nums.index(oth)])
        except:
            return sstr(str.__add__(self, other))
Output:
>>> a = sstr("two") >>> a 'two' >>> a + "three" 'five' >>> a += "two" >>> a 'four' >>> a += "three" # resut is out of "num" range >>> a 'fourthree'
Indeed this is just a starting point, much more effort would be needed to make it as powerful as INTERCAL...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is python an interpreted language or a compiled language? GouravDas 1 1,984 Jun-23-2020, 10:38 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020