Rows class constructor question

50 views
Skip to first unread message

Vlad

unread,
May 20, 2019, 11:14:45 PM5/20/19
to web2py-users
I must be missing something very basic here... 

Want to create an empty Rows object, and then to append to it sets of rows from various sql queries with the same structure of the result sets. 

The first line in the code (in a controller) is
     rows = Rows()
with an intention of creating an empty Rows object. 

This gives an error: <class 'NameError'> name 'Rows' is not defined

Why is that? Why can't I create an empty Rows object? 



Leonel Câmara

unread,
May 21, 2019, 8:31:36 AM5/21/19
to web2py-users
You need to import Rows from pydal.objects

Eliezer (Vlad) Tseytkin

unread,
May 21, 2019, 9:14:23 AM5/21/19
to web...@googlegroups.com
Oops, I thought that if DAL is used extensively and all works already, all the proper classes are already imported by the environment (otherwise how can I run queries and do other db-related stuff without importing those classes explicitly??) How can rows=db.select work and return 'rows', which is an instance of the class Rows, if the class Rows is not imported and thus not defined?? What am I missing? 


On Tue, May 21, 2019, 8:31 AM Leonel Câmara <leonel...@gmail.com> wrote:
You need to import Rows from pydal.objects

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/C31RurYvstc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/6ea3bcf5-d419-453d-8a5d-837af3d661f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Leonel Câmara

unread,
May 21, 2019, 9:22:53 AM5/21/19
to web2py-users
If you have a module called peartree.py which has only this

class Pear:
   
pass


def shake():
   
return Pear()


If in another module you do:

import peartree


mypear
= peartree.shake()


mypear will be an instance of Pear, however Pear is not defined in this context and you cannot use the class directly without going through the peartree namespace.

Vlad

unread,
May 21, 2019, 10:33:03 AM5/21/19
to web2py-users
Leonel,
in your example, if "import peartree" doesn't bring in Pear (and thus Pear needs to be imported explicitly) - then... How is Pear accessible at all? I mean, if Pear is not accessible - then what's the difference between direct usage and indirect usage? I guess I am missing something basic on how namespaces work, but it seems straightforward to me: if rows=db.select() works and rows is known to be an instance of the class Rows, and Rows is recognized as a class and can be used, just... not directly??? I mean, it should be either visible or invisible, and if it's visible - everything about it is visible, and if it's invisible - everything about it should be invisible... So, how can it be that it's visible and recognized as a class, yet the details of this class are hidden - so it can be used partially but not fully?? I am mystified.  
Thank you for your patience :) 

villas

unread,
May 21, 2019, 11:52:23 AM5/21/19
to web2py-users
rows = pydal.objects.Rows()

Vlad

unread,
May 21, 2019, 12:48:56 PM5/21/19
to web2py-users
Yeah, I am just trying to understand why it doesn't work without explicit import. Missing basics here:
if rows=db.select() works and rows is known to be an instance of the class Rows, and Rows is recognized as a class and can be used to some extent (obviously, I am using it already, in rows=db.select() and beyond, without importing explicitly), what does it mean then that I can't use it? I mean, it's either visible or invisible. If it's already visible as a specific class, without being imported, why are its methods still invisible and require explicit import statement? I am suspecting that I am missing something very basic about namespaces. Earlier Leonel explained it, but I didn't understand the explanation. 

Leonel Câmara

unread,
May 21, 2019, 2:08:57 PM5/21/19
to web2py-users
It basically has to do with python namespaces. You did not import Rows into your current namespace. The definition is there in the context where the select rows were created but in your namespace you could have another Rows definition that is quite different or none at all. Web2py imports a lot of stuff automatically so I can see how that can create confusion when something isn't automatically available but that's just the way regular python works.

Eliezer (Vlad) Tseytkin

unread,
May 21, 2019, 5:47:04 PM5/21/19
to web...@googlegroups.com
Got it,
Thank you very much! 

On Tue, May 21, 2019, 2:09 PM Leonel Câmara <leonel...@gmail.com> wrote:
It basically has to do with python namespaces. You did not import Rows into your current namespace. The definition is there in the context where the select rows were created but in your namespace you could have another Rows definition that is quite different or none at all. Web2py imports a lot of stuff automatically so I can see how that can create confusion when something isn't automatically available but that's just the way regular python works.

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/C31RurYvstc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages