Re: Could someone help me understand the finer points of import

2009-01-04 Thread Graham Dumpleton



On Jan 5, 1:29 pm, Mark Jones  wrote:
> For the discussion below, ALL the code is in .../intomec/tenq
>
> I wrote some code in tests.py like so:
>
>    from tenq.models import *
>         self.expectedValue = Answers((1,2,3,4))
>
> within the to_python() method of AnswersField in models.py:
>     def to_python(self, _value):
>         print "_value.__class__ =",_value.__class__
>         if isinstance(_value, Answers):
>             return _value
>
> isinstance(_value, Answers) fails for the object created in tests.py
> because the
>
> >>>_value.__class__ = 
> >>>_value.__class__ = 
>
> when I change tests.py to
> from intomec.tenq.models import *
>         self.expectedValue = Answers((1,2,3,4))
>
> I get>>>_value.__class__ = 
> >>>_value.__class__ = 
>
> and isinstance doesn't fail.
>
> What I can't figure out is, why do both imports work, and why do they
> generate different results.  It seems to me the name of the class is
> the name of the class, and either the import should fail or they
> should both yield the same name.
>
> Anyone have any ideas on why this is?  I'm not even sure why the
> second from statement works, since I'm already in the intomec folder
> when I run
>
> python manage.py test

You get this because the directory where your settings.py file is, and
the parent directory of that directory, are both effectively listed in
sys.path.

Actually, the latter isn't in sys.path if you look, but it was at one
point while package __init__.py of the site was imported. Even though
the directory is removed from sys.path after that, that the package
has already been loaded means that imports via site name still work.

See my prior desription of what Django manage.py does in:

  
http://groups.google.com/group/django-users/browse_frm/thread/24deb095a2b2e450/f8e703bbb96e9e88

Graham
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Could someone help me understand the finer points of import

2009-01-04 Thread Daniel Roseman

On Jan 4, 9:29 pm, Mark Jones  wrote:
> For the discussion below, ALL the code is in .../intomec/tenq
>
> I wrote some code in tests.py like so:
>
>    from tenq.models import *
>         self.expectedValue = Answers((1,2,3,4))
>
> within the to_python() method of AnswersField in models.py:
>     def to_python(self, _value):
>         print "_value.__class__ =",_value.__class__
>         if isinstance(_value, Answers):
>             return _value
>
> isinstance(_value, Answers) fails for the object created in tests.py
> because the
>
> >>>_value.__class__ = 
> >>>_value.__class__ = 
>
> when I change tests.py to
> from intomec.tenq.models import *
>         self.expectedValue = Answers((1,2,3,4))
>
> I get>>>_value.__class__ = 
> >>>_value.__class__ = 
>
> and isinstance doesn't fail.
>
> What I can't figure out is, why do both imports work, and why do they
> generate different results.  It seems to me the name of the class is
> the name of the class, and either the import should fail or they
> should both yield the same name.
>
> Anyone have any ideas on why this is?  I'm not even sure why the
> second from statement works, since I'm already in the intomec folder
> when I run
>
> python manage.py test

I can't really understand your problem, I'm afraid - in particular I
can't see what is generating those two output lines in either case.
Could you post a bit more code (preferably on dpaste.com)?

In the meantime, the best reference on import (as on many other
intricacies of Python) is the effbot, Frederik Lundh:
http://effbot.org/zone/import-confusion.htm
Does that help answer your question at all?

And to answer your final question, the reason the second import works
is because manage.py adds the parent of the current directory to the
pythonpath automatically.
--
DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Could someone help me understand the finer points of import

2009-01-04 Thread Mark Jones

For the discussion below, ALL the code is in .../intomec/tenq

I wrote some code in tests.py like so:

   from tenq.models import *
self.expectedValue = Answers((1,2,3,4))

within the to_python() method of AnswersField in models.py:
def to_python(self, _value):
print "_value.__class__ =",_value.__class__
if isinstance(_value, Answers):
return _value

isinstance(_value, Answers) fails for the object created in tests.py
because the
>>>_value.__class__ = 
>>>_value.__class__ = 


when I change tests.py to
from intomec.tenq.models import *
self.expectedValue = Answers((1,2,3,4))

I get
>>>_value.__class__ = 
>>>_value.__class__ = 
and isinstance doesn't fail.

What I can't figure out is, why do both imports work, and why do they
generate different results.  It seems to me the name of the class is
the name of the class, and either the import should fail or they
should both yield the same name.

Anyone have any ideas on why this is?  I'm not even sure why the
second from statement works, since I'm already in the intomec folder
when I run

python manage.py test

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---