python list in fb ?

General discussion for topics related to the FreeBASIC project or its community.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

python list in fb ?

Post by bluatigro »

i want a python like list type in fb
so i can do

Code: Select all

  dim as list lst 
  lst = [ 1 , 2 , .3 , "4" ] 
  list.append( "test" )
  list.append( 1 )
  list.append( 0.5 )
  lst.append( [ "test" , 1 , 0.5 ] )
  if istype( lst[ 4 ] , string ) then 
  if istype( lst[ 4 ] , double ) then 
  if istype( lst[ 4 ] , integer ) then 
  if istype( lst[ 4 ] , list ) then 
  if istype( lst[ 4 ] , sometype ) then 
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: python list in fb ?

Post by St_W »

That is impossible in FreeBasic as basic types (like Integer, String) are not objects, meaning their type is not known at runtime. If you would like to have something like that you have to encapsulate the primitive types in wrapper objects. Other objects could be used as-is.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: python list in fb ?

Post by caseih »

It's definitely not impossible. There are several ways to do it. One would be to require the end user to specify the type in some way, perhaps using an enumeration constant or string, when appending the item (by any ptr) to the list. Then the caller could inquire what type an element is when accessing it, and cast it appropriately.

Another way would be to make the list item class have overridden methods to accept a certain subset of known types. To my knowledge FB does not have run-time typing information (RTTI) like C++ does, so truly generic code is more difficult.

So yes, it can theoretically be done.
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: python list in fb ?

Post by St_W »

caseih wrote:There are several ways to do it. One would be to require the end user to specify the type in some way, perhaps using an enumeration constant or string, when appending the item (by any ptr) to the list. Another way would be to make the list item class have overridden methods to accept a certain subset of known types.
It is impossible *that way*. I did not claim lists in general are impossible to implement in FB. There do exist several implementations btw, like e.g mdTypes.
Specifying a type somehow additionally is of course possible, but not the way it works in Python or in the pseudo-code above (no type specifications needed for adding items to list). The workaround using overridden methods is possible too, but won't work for adding multiple (varying count) values at once as in the pseudo-code.
caseih wrote:To my knowledge FB does not have run-time typing information (RTTI) like C++ does, so truly generic code is more difficult.
FB does have some limited RTTI, but you have to extend Object. That's why it won't work for primitive types like Integer or String (or other types not extending Object).
marcov
Posts: 3454
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: python list in fb ?

Post by marcov »

Some explanation of jargon, in case it is too Delphi centric, though C#'s similar types have pulled most mainstream:

Variant = variadic type (COM olevariant like)

open array = accepts an array of a certain type, and receives the length as implicit parameter. (some languages also pass a pointer to a stack struct with pointer to element 0 and length to keep it one parameter).

property Pseudo field that abstracts between a "true" field (data member) of a struct or object or a getter/setter pair. Indexed (Array) types exist too.

default property the property accessed if you dereference only the class.

array of const Open array type that accepts all element types and pushes typinfo per parameter. Typesafe(r) variant of C variadic parameters.
bluatigro wrote:i want a python like list type in fb
lst = [ 1 , 2 , .3 , "4" ]
Needs constant/literal "array of variant", and a default property of what I assume is object "lst" that can receive an dynamic array of variant. Either directly or via an open array of variant. CHanging the syntax to lst.items= [1,2,3 ] etc would solve ambiguity.
list.append( "test" )
list.append( 1 )
list.append( 0.5 )
The above can be done by overloading alone;
lst.append( [ "test" , 1 , 0.5 ] )
Multiple types is hard with overloading -> needs array of variant literal or open array variant.
if istype( lst[ 4 ] , string ) then
if istype( lst[ 4 ] , double ) then
if istype( lst[ 4 ] , integer ) then
if istype( lst[ 4 ] , list ) then
While one would instinctively assume this is RTTI, if lst separately stores type (or as part of a variadic type) separately, it would need RTTI.
if istype( lst[ 4 ] , sometype ) then
Arbritary (custom) types are very difficult. This would need RTTI and many things more.
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Re: python list in fb ?

Post by MOD »

There's an old project of mine: Variant
This allows you to store strings, integers and doubles in one datatype (you have to remove the ucase and lcase overloads from the Variant.bi as FB changed these recently). In combination with mdTypes you can achieve what you want:

Code: Select all

#Include Once "Variant.bi"
#Include Once "md/util/mdList.bi"
 
mdListDeclare(Variant)
 
Dim As mdList(Variant) list
 
list.add("test")
list.add(1)
list.add(0.5)
 
Dim As Variant temp
ForEach(Variant, temp In list)
    Print temp, temp.getRealType()
NextEach
 
Sleep
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: python list in fb ?

Post by BasicCoder2 »

This highlights an issue I have with Python in terms of translating between Python and FreeBASIC. It is rather difficult to do compared with C. The LISP like way Python handles lists may be powerful but difficult for me. LISP is not a human like language compared with BASIC.
.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: python list in fb ?

Post by caseih »

I'm not sure any computer language is truly human-like or intuitive. It's all based on what you have learned and how you think. Apple tried to make a very "natural" programming language called AppleScript and it's rather horrible. It reads well but the "natural language" syntax is actually quite rigid which makes writing code in the language a bit of a bother.

The list as LISP presents it, and to a lesser degree, Python, is a very powerful concept and becomes quite natural. It's exceptionally easy to create many different kinds of data structures with lists. Between lists and hash tables, many use cases are covered, and without writing a lot of boilerplate code and classes.

I'm curious, what part of lists is difficult for you?

Question here. St_W makes the point that FB does have some limited RTTI that comes from extending Object. Are all UDT classes extending Object or is this something that has to be explicit? If the latter, what advantages and disadvantages come from extending Object vs plain UDT? I'll have to explore this a bit. If there are few disadvantages, maybe all UDTs should implicity extend Object. That would mean a list class could take objects of any type by reference and store them (basically pointers under the hood).
marcov
Posts: 3454
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: python list in fb ?

Post by marcov »

caseih wrote:

The list as LISP presents it, and to a lesser degree, Python, is a very powerful concept and becomes quite natural. It's exceptionally easy to create many different kinds of data structures with lists. Between lists and hash tables, many use cases are covered, and without writing a lot of boilerplate code and classes.
That is still not a reason to have it in language. Most mature languages have STL like container types that do the same.

And while it saves on declaring struct to group data, in real applications you will rarely have most of such lists hardcoded, but rather they are tables in some database (and a runtime layout), which can represented perfectly fine with the array of variants we are emulting it with here.

So while in it is great for simple looking examples, I've always considered the in-language of lists an overrated feature.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: python list in fb ?

Post by caseih »

Not sure I'm understanding what you are saying. Both LISP and Python are "mature" languages. I think what you meant to say was in a statically-typed language such as C++ or FB, we have container classes and other data structures which are a nice fit for more formal declarations of variable type.

Your last comment about lists being "great for simple examples" is very strange. Good for simple examples only? You can't be serious. I use lists in *every* Python program I've ever written and I've never seen a Python program that did not use them. They are syntactically equivalent to dynamic arrays, except that in Python and LISP they can contain items of any type. They can be nested and combined with hash tables and classes (which are themselves actually hash tables) to build any kind of formal structure you want and tied to databases. Database calls return lists of the query data results. Again I'm not sure what you're referring to.

Are you referring to the fact that in languages like FB or C++ there are other data types that would be more appropriate than lists for many things? If so, I'll agree with that. In a dynamic language, of course, the situation is quite different because the constraints are different. That said, linked lists (singly and doubly- linked) are widely and commonly used in all languages for certain tasks. It's a fundamental data structure that all programmers are taught about. The Linux kernel uses them extensively to manage things like run queues, memory allocations, etc.

I think we've established that the dynamic nature of Python's lists cannot be replicated easily in FB without a lot of helper wrapper classes or just using pointers.
marcov
Posts: 3454
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: python list in fb ?

Post by marcov »

caseih wrote:Not sure I'm understanding what you are saying. Both LISP and Python are "mature" languages.
So? Being a mature language doesn't preclude questioning its design decisions?
I think what you meant to say was in a statically-typed language such as C++ or FB, we have container classes and other data structures which are a nice fit for more formal declarations of variable type.
Maybe, but no.
Your last comment about lists being "great for simple examples" is very strange. Good for simple examples only? You can't be serious. I use lists in *every* Python program I've ever written and I've never seen a Python program that did not use them. They are syntactically equivalent to dynamic arrays, except that in Python and LISP they can contain items of any type.
I get a feeling you didn't read the message thoroughly, but just replied on what you considered a LISP or Python critique.In reality though, I'm questioning what having them in-language as first class type is improving beyond a library solution.

So how much do the the slight syntax shortenings help for sizable say server application that talks to a backend? This because a library variadic solution can access the typeinfo in the result dataset from the SQL or bigdata server. (and this is where a python module will also load the typeinfo from for the interpreter's list construction too ) Since the db libraries also use such constructs like variadic arrays (or more complex optimized solutions).
I think we've established that the dynamic nature of Python's lists cannot be replicated easily in FB without a lot of helper wrapper classes or just using pointers.
That was never the question. The question is how much Python really adds beyond what a library construct in C++, C# or Delphi would do.

Yes, the Python solution will be slightly less typing here and there, but that is not really what matters.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: python list in fb ?

Post by BasicCoder2 »

caseih wrote:I'm not sure any computer language is truly human-like or intuitive. It's all based on what you have learned and how you think.
Yes I guess that is why I find Python harder to learn as it does things differently to the way I usually think about representing a problem.
'm curious, what part of lists is difficult for you?
My first language was Assembler (Z80, 6502, 68000, 8086 and later 80386) along with BASIC and C. I am an occasional on and off hobby programmer and being self taught from non technical books there are very large gaps in what I know. I understand LISTS in the context of those languages.

I would like to magically have the knowledge you have but have not the time now or the motivation to make the time to learn them and thus limit my programming hobby to using FreeBASIC. For anyone starting out it would be wise to become multilingual with regards the most used languages. In my day all I had was assembler, BASIC or C teach yourself books.

However how to implement python lists in fb interested me in terms of being able to translate both ways.
.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: python list in fb ?

Post by dodicat »

I downloaded python34.dll
All these pylist functions are contained within, as can be seen from the python34.def file.
If you desperately wanted to use them then perhaps try to create a binding to freebasic (.bi file).
However, if the .dll was written in C++ (I don't know!), then it might not be possible.
Here are some of the pylist functions:
PyListRevIter_Type
PyList_Append
PyList_AsTuple
PyList_ClearFreeList
PyList_Fini
PyList_GetItem
PyList_GetSlice
PyList_Insert
PyList_New
PyList_Reverse
PyList_SetItem
PyList_SetSlice
PyList_Size
PyList_Sort
PyList_Type
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: python list in fb ?

Post by caseih »

marcov wrote:
I think we've established that the dynamic nature of Python's lists cannot be replicated easily in FB without a lot of helper wrapper classes or just using pointers.
That was never the question. The question is how much Python really adds beyond what a library construct in C++, C# or Delphi would do.
Yes, the Python solution will be slightly less typing here and there, but that is not really what matters.
Umm, no that wasn't the question. At least it wasn't the question that the OP asked, and the one that I answered in that last bit. The original question was "i want a python like list type in fb so i can do [example in python]." Whether list is valid as a language-level primitive never really entered into it, except to use Python and Lisp as examples of languages that have such lists and the benefits that come from it.

As to LIST being unnecessary, well everything is syntactic sugar. FB arrays are similar, though I'd say they are an important part of the language of FB. Really everything we do on a computer can boil down to one instruction. So eliminate all the rest? At least in the case of Python and especially Lisp the idea that list should be removed from the language is a bit much (ludicrous in the case of Lisp!) I don't think I or the OP or anyone was advocating adding lists to FB as first-class types. Merely the discussion was about whether or not a list class could emulate some of Python's behavior and also how useful lists are.
Last edited by caseih on Mar 20, 2016 4:42, edited 4 times in total.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: python list in fb ?

Post by caseih »

dodicat wrote:However, if the .dll was written in C++ (I don't know!), then it might not be possible.
The dll is written in C and exports a fairly nice C api. There're about a dozen header files. I've often thought FB might make a good language for extending Python. The interesting thing about using python.dll is that there's no difference between writing an add-on for Python (a compiled class or module as it were) and embedding Python itself in your program.

There's a caveat when trying to use python.dll on Windows and that is that Python is compiled with a specific version of Visual Studio and links against its specific version of msvcrt. Whereas FB (and GCC) link against the old msvcrt.dll that the Mingw runtime uses. Thus there can be conflicts when mixing dlls that refer to different msvcrts. Things that manipulate files or refer to the err variables will not work right, if at all. I'm not sure if FreeBASIC can emit code that can be built and linked with Visual Studio or not. There's been talk of moving Mingw to target something a little more modern but there are issues with redistribution of the dll.
Post Reply