Re: [Tutor] Programming Ideas, need some focus

2008-01-21 Thread Benjamin Eckenfels
On Fri, Jan 18, 2008 at 10:46:12AM -0500, "Simón A. Ruiz" wrote:
> I'll second that.


Me, too. Thanks for that hint. I currently have a great time with python 
challenge. I recommend it for everybody who like me has just finished 
the python basics an now is looking for an entertaining way to get 
familiar with the language and the most important modules.

Cheers
-- 
Benjamin Eckenfels

OpenPGP
Key id: CF56E489
Key fingerprint = 386D CBE1 0833 4C12 2871  F51E 839D 18EF CF56 E489
Public Key: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xCF56E489



signature.asc
Description: Digital signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] float_info

2008-01-21 Thread Kent Johnson
This is probably of interest to Dick Moores at least...
 From What's New in Python 2.6
http://docs.python.org/dev/whatsnew/2.6.html

[Note: A release date has not been set for Python 2.6]

A new variable in the sys module, float_info, is an object containing 
information about the platform’s floating-point support derived from the 
float.h file. Attributes of this object include mant_dig (number of 
digits in the mantissa), epsilon (smallest difference between 1.0 and 
the next largest value representable), and several others. (Contributed 
by Christian Heimes.)


Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
class Foo:
  '''Represents a foo'''
  def __init__(self, name):
'''Initializes the person's data'''
self.name = name
print '(Initializing %s)' % self.name
self.ot = Bar(self.name)
print '(After Other - %s)' % self.name

class Bar:
def __init__(self, name):
self.name = name
print 'Other', self.name
self.name.pop('srv')
print 'Other (Changed)', self.name

dict = { "srv" : "why", "goo" : "sticky" }
foo = Foo(dict)
print foo.name


Why does the pop in the Bar class nuke the srv k & v from Foo.name as well?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread Kent Johnson
John Morris wrote:

> Why does the pop in the Bar class nuke the srv k & v from Foo.name 
>  as well?

Because they are both names for the same dict.

Assignment in Python does not copy values; it binds a name to a value. 
Some good references:
http://effbot.org/zone/python-objects.htm
http://groups.google.com/group/comp.lang.python/browse_thread/thread/56e7d62bf66a435c/

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
Does it have something to do with:
http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm

And if so can anyone explain it a bit for me, I'm being slow tonight.

I thought each class got it's own namespace and this sharing of mutable
objects is confusing me.

Thanks.


On Jan 21, 2008 9:16 PM, John Morris <[EMAIL PROTECTED]> wrote:

> class Foo:
>   '''Represents a foo'''
>   def __init__(self, name):
> '''Initializes the person's data'''
> self.name = name
> print '(Initializing %s)' % self.name
> self.ot = Bar(self.name)
> print '(After Other - %s)' % self.name
>
> class Bar:
> def __init__(self, name):
> self.name = name
> print 'Other', self.name
> self.name.pop('srv')
> print 'Other (Changed)', self.name
>
> dict = { "srv" : "why", "goo" : "sticky" }
> foo = Foo(dict)
> print foo.name
>
>
> Why does the pop in the Bar class nuke the srv k & v from Foo.name as
> well?
>
>


-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
Thanks,

so I could/should do

self.ot = Bar(self.name.copy()) instead


On Jan 21, 2008 9:25 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> John Morris wrote:
>
> > Why does the pop in the Bar class nuke the srv k & v from Foo.name
> >  as well?
>
> Because they are both names for the same dict.
>
> Assignment in Python does not copy values; it binds a name to a value.
> Some good references:
> http://effbot.org/zone/python-objects.htm
>
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/56e7d62bf66a435c/
>
> Kent
>



-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Fouhy
On 22/01/2008, John Morris <[EMAIL PROTECTED]> wrote:
> I thought each class got it's own namespace and this sharing of mutable
> objects is confusing me.

Each class gets its own namespace, but names are different from
objects.  For example:

>>> x = [1, 2, 3]
>>> y = x
>>> y.append(4)
>>> x
[1, 2, 3, 4]

In this case, x and y are both different names for the same object.
Classes increase the name space, but they don't change the fact that
in python, assignment is just giving something a new name.

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread Kent Johnson
John Morris wrote:
> Does it have something to do with:
> http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm
> 
> And if so can anyone explain it a bit for me, I'm being slow tonight.
> 
> I thought each class got it's own namespace and this sharing of mutable 
> objects is confusing me.

It has nothing to do with namespaces, it is the semantics of assignment 
that is your problem. What you have done is essentially the same as 
this, just obfuscated ;-)

In [1]: d = { "srv" : "why", "goo" : "sticky" }
In [2]: n = d
In [3]: n.pop('srv')
Out[3]: 'why'
In [4]: d
Out[4]: {'goo': 'sticky'}

Look at the references I sent (or wait for someone else with more energy 
than I to explain :-) )

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
So this seems like it will make scope/namespaces a bit interesting...

Any good references on why this is this way?
I.e., why assignment passes across scopes instead of copy.
Or is it just explicit versus implicit?



On Jan 21, 2008 9:32 PM, John Fouhy <[EMAIL PROTECTED]> wrote:

> On 22/01/2008, John Morris <[EMAIL PROTECTED]> wrote:
> > I thought each class got it's own namespace and this sharing of mutable
> > objects is confusing me.
>
> Each class gets its own namespace, but names are different from
> objects.  For example:
>
> >>> x = [1, 2, 3]
> >>> y = x
> >>> y.append(4)
> >>> x
> [1, 2, 3, 4]
>
> In this case, x and y are both different names for the same object.
> Classes increase the name space, but they don't change the fact that
> in python, assignment is just giving something a new name.
>
> --
> John.
>



-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread Kent Johnson
John Morris wrote:
> Thanks,
> 
> so I could/should do
> 
> self.ot = Bar(self.name.copy()) instead

Yes, if you want a copy you have to ask for it.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
Ahhh. so namespaces are scoped, not objects...
You have to keep your objects separate (and make copies when needed), Python
just keeps namespaces (names that refer to an object) scoped according to
it's rules:
http://docs.python.org/ref/naming.html

So if you create an object way up in terms of scope (global), then all
python does is handle what names are available in a given scope to refer to
it. If you want a separate object you have to take care of that yourself.
Efficient. Massive potential for gotchas, especially with some of python's
cleverness in terms of scoping rules.

Sort of. I see some light beginning to dawn ;).

On Jan 21, 2008 9:16 PM, John Morris <[EMAIL PROTECTED]> wrote:

> class Foo:
>   '''Represents a foo'''
>   def __init__(self, name):
> '''Initializes the person's data'''
> self.name = name
> print '(Initializing %s)' % self.name
> self.ot = Bar(self.name)
> print '(After Other - %s)' % self.name
>
> class Bar:
> def __init__(self, name):
> self.name = name
> print 'Other', self.name
> self.name.pop('srv')
> print 'Other (Changed)', self.name
>
> dict = { "srv" : "why", "goo" : "sticky" }
> foo = Foo(dict)
> print foo.name
>
>
> Why does the pop in the Bar class nuke the srv k & v from Foo.name as
> well?
>
>


-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Fouhy
On 22/01/2008, John Morris <[EMAIL PROTECTED]> wrote:
> So if you create an object way up in terms of scope (global), then all
> python does is handle what names are available in a given scope to refer to
> it. If you want a separate object you have to take care of that yourself.
> Efficient. Massive potential for gotchas, especially with some of python's
> cleverness in terms of scoping rules.

There is potential for gotchas, but it's reduced by the fact that
integers and strings are both immutable.  For example:

>>> x = 'foo'
>>> y = x
>>> y is x   # this tests whether x and y are different names for the
same object
True
>>> y += 'bar'  # this is equivalent to: y = y + 'bar'
>>> y is x
False
>>> y, x
('foobar', 'foo')

(this is why there is no '.append()' method for strings)

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread Kent Johnson
John Morris wrote:
> Ahhh. so namespaces are scoped, not objects...

I would say names are scoped, but I guess namespaces are too.

> You have to keep your objects separate (and make copies when needed), 

Yes

> Python just keeps namespaces (names that refer to an object) scoped 
> according to it's rules:
> http://docs.python.org/ref/naming.html
> 
> So if you create an object way up in terms of scope (global), then all 
> python does is handle what names are available in a given scope to refer 
> to it. If you want a separate object you have to take care of that 
> yourself. Efficient. 

Yes. Assignment is not copying, it is name-binding.

> Massive potential for gotchas, especially with some 
> of python's cleverness in terms of scoping rules.

In my experience it is rare to actually need a copy of an object, and 
usually pretty clear when I do. The real hurdle is getting your mental 
model in sync with what Python is actually doing, rather than what you 
think it should be doing.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionaries, objects and scoping...

2008-01-21 Thread John Morris
Thanks. I think this is understood better now.

Thanks to everyone for their help. I was running low on ways to express this
for clearer understanding.

Awesomeness once again from [EMAIL PROTECTED]

- John

On Jan 21, 2008 10:18 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> John Morris wrote:
> > Ahhh. so namespaces are scoped, not objects...
>
> I would say names are scoped, but I guess namespaces are too.
>
> > You have to keep your objects separate (and make copies when needed),
>
> Yes
>
> > Python just keeps namespaces (names that refer to an object) scoped
> > according to it's rules:
> > http://docs.python.org/ref/naming.html
> >
> > So if you create an object way up in terms of scope (global), then all
> > python does is handle what names are available in a given scope to refer
> > to it. If you want a separate object you have to take care of that
> > yourself. Efficient.
>
> Yes. Assignment is not copying, it is name-binding.
>
> > Massive potential for gotchas, especially with some
> > of python's cleverness in terms of scoping rules.
>
> In my experience it is rare to actually need a copy of an object, and
> usually pretty clear when I do. The real hurdle is getting your mental
> model in sync with what Python is actually doing, rather than what you
> think it should be doing.
>
> Kent
>



-- 
John Morris
[EMAIL PROTECTED]
"Do nothing which is of no use." -- Miyamoto Musashi
http://profile.mygamercard.net/nerdality";>
http://card.mygamercard.net/gbar/abyss/nerdality.gif"; border=0>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor