Posts: 4,647
Threads: 1,494
Joined: Sep 2016
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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
Apr-02-2017, 10:47 AM
(This post was last modified: Apr-02-2017, 10:47 AM by ichabod801.)
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
Posts: 5,151
Threads: 396
Joined: Sep 2016
Apr-02-2017, 01:31 PM
(This post was last modified: Apr-02-2017, 01:32 PM by metulburr.)
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:
Posts: 2,953
Threads: 48
Joined: Sep 2016
Non-empty object is always True.
Posts: 331
Threads: 2
Joined: Feb 2017
(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
Posts: 470
Threads: 29
Joined: Sep 2016
(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?
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Apr-03-2017, 01:55 AM
(This post was last modified: Apr-03-2017, 02:08 AM by Skaperen.)
(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.
Posts: 2,342
Threads: 62
Joined: Sep 2016
(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.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Apr-03-2017, 04:27 AM
(This post was last modified: Apr-03-2017, 04:27 AM by wavic.)
(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
Posts: 331
Threads: 2
Joined: Feb 2017
(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...
|