Re: [Tutor] Immutable objects

2010-08-19 Thread Alan Gauld
Nitin Das nitin@gmail.com wrote class mymut(object): def __setattr__(self,k,v): if hasattr(self,k): if self.__dict__.get(k) == None: self.__dict__[k] = v Do you need to look up the dict? Surely you could just use if self.k is None self.k = v which

[Tutor] question about a exercise.

2010-08-19 Thread Roelof Wobben
Hello, I follow the online book How to think like a computer scientist. But now I have a problem with a exercise. This is not homework. The exercise is : Using a text editor, create a Python script named tryme3.py . Write a function in this file called nine_lines that uses

[Tutor] AUTO: James D Mcclatchey is out of the office. (returning 09/07/2010)

2010-08-19 Thread James D Mcclatchey
I am out of the office until 09/07/2010. I will respond to your message when I return. Note: This is an automated response to your message Tutor Digest, Vol 78, Issue 83 sent on 8/19/10 3:00:03. This is the only notification you will receive while this person is away. *IMPORTANT NOTICE:

Re: [Tutor] Immutable objects

2010-08-19 Thread Steven D'Aprano
On Thu, 19 Aug 2010 01:24:11 pm Nitin Das wrote: Hello, Can somebody help me how to make immutable objects in python. I have seen overriding the __new__, __setattr__ methods.. but not comfortable with how to use them to make the object immutable. thanks in advance --nitin I have

Re: [Tutor] question about a exercise.

2010-08-19 Thread Joel Goldstick
Can you copy and paste the exact error message you receive in the traceback when you run your program. Also, copy the complete program here since it is only a handful of lines On Thu, Aug 19, 2010 at 7:31 AM, Roelof Wobben rwob...@hotmail.com wrote: Hello, I follow the online book How to

Re: [Tutor] Immutable objects

2010-08-19 Thread Peter Otten
Nitin Das wrote: class mymut(object): def __setattr__(self,k,v): if hasattr(self,k): if self.__dict__.get(k) == None: self.__dict__[k] = v else: raise TypeError(Cant Modify Attribute Value) else: raise

Re: [Tutor] question about a exercise.

2010-08-19 Thread Roelof Wobben
Hello, I don't see a traceback only this output. regel 1 regel 2 script terminated. And the code I gave you was the whole code because Im still working on this one. I have learned to do one problem at the time so in mu opion first make the part which prints out 3 lines

Re: [Tutor] Immutable objects

2010-08-19 Thread Peter Otten
Peter Otten wrote: Nitin Das wrote: class mymut(object): def __setattr__(self,k,v): if hasattr(self,k): if self.__dict__.get(k) == None: self.__dict__[k] = v else: raise TypeError(Cant Modify Attribute Value) else:

Re: [Tutor] question about a exercise.

2010-08-19 Thread Alan Gauld
Roelof Wobben rwob...@hotmail.com wrote I don't see a traceback only this output. That looks about right to me, what did you expect to see that was different? regel 1 regel 2 script terminated. = I have learned to do one problem at

Re: [Tutor] Multiple file open

2010-08-19 Thread nitin chandra
Hello All, Please guide with the syntax. All beginners tutorials on the web teach the syntax of python.. I am unsure what your questions is. My bad with code. below is the existing program with Formula A (Mean). Formula B will be Extrapolation, also I have not been able to do

Re: [Tutor] Immutable objects

2010-08-19 Thread Nitin Das
Thanks guys, NamedTuple implementation is preety nice solution. --nitin On Thu, Aug 19, 2010 at 6:28 PM, Peter Otten __pete...@web.de wrote: Peter Otten wrote: Nitin Das wrote: class mymut(object): def __setattr__(self,k,v): if hasattr(self,k): if

[Tutor] List comprehension for dicts?

2010-08-19 Thread Pete
Hi, I've been reading up on list comprehensions lately, all userful and powerful stuff - trying to wrap my brain around it :) As the examples all seem to relate to lists, I was wondering if there is an elegant similar way to apply a function to all keys in a dictionary? (without looping over

Re: [Tutor] List comprehension for dicts?

2010-08-19 Thread Peter Otten
Pete wrote: Hi, I've been reading up on list comprehensions lately, all userful and powerful stuff - trying to wrap my brain around it :) As the examples all seem to relate to lists, I was wondering if there is an elegant similar way to apply a function to all keys in a dictionary?

Re: [Tutor] List comprehension for dicts?

2010-08-19 Thread Wayne Werner
On Thu, Aug 19, 2010 at 10:02 AM, Vince Spicer vi...@vinces.ca wrote: Hey you can use list comprehension here age_dict = { 'pete': 42, 'ann': 25, 'carl': 30, 'amanda': 64 } you can create a dict from a list of tuples and you can access the dict as a list of tuples by accessing its items

Re: [Tutor] List comprehension for dicts?

2010-08-19 Thread Shashwat Anand
On Thu, Aug 19, 2010 at 8:21 PM, Pete pkoe...@xs4all.nl wrote: Hi, I've been reading up on list comprehensions lately, all userful and powerful stuff - trying to wrap my brain around it :) As the examples all seem to relate to lists, I was wondering if there is an elegant similar way to

Re: [Tutor] List comprehension for dicts?

2010-08-19 Thread Steven D'Aprano
On Fri, 20 Aug 2010 01:40:54 am Wayne Werner wrote: age_dict = dict([(key.upper(), value) for key,value in age_dict.items()]) This is a bad place to use a list comprehension. This will create a list of values first and then create a dict from that list, so now you have a list floating

Re: [Tutor] List comprehension for dicts?

2010-08-19 Thread Dave Angel
(You top-posted, so now I have to delete the older part) Vince Spicer wrote: Hey you can use list comprehension here age_dict = { 'pete': 42, 'ann': 25, 'carl': 30, 'amanda': 64 } you can create a dict from a list of tuples and you can access the dict as a list of tuples by accessing its

[Tutor] flow problem with a exercise

2010-08-19 Thread Roelof Wobben
Hello, I have this exercise: Now write the function is_odd(n) that returns True when n is odd and False otherwise. Include doctests for this function as you write it. Finally, modify it so that it uses a call to is_even to determine if its argument is an odd integer. So I thought

Re: [Tutor] flow problem with a exercise

2010-08-19 Thread Joel Goldstick
On Thu, Aug 19, 2010 at 3:01 PM, Roelof Wobben rwob...@hotmail.com wrote: Hello, I have this exercise: Now write the function is_odd(n) that returns True when n is odd and Falseotherwise. Include doctests for this function as you write it. Finally, modify it so that it uses a call to

Re: [Tutor] flow problem with a exercise

2010-08-19 Thread Wayne Werner
On Thu, Aug 19, 2010 at 2:01 PM, Roelof Wobben rwob...@hotmail.com wrote: snip def is_odd(argument): uitkomst=is_even(argument) return uitkomst even=is_odd(1) ; if even==True : print Even getal if even==False: print Oneven getal But now I get this error message : return

Re: [Tutor] flow problem with a exercise

2010-08-19 Thread Alex Hall
On 8/19/10, Roelof Wobben rwob...@hotmail.com wrote: Hello, I have this exercise: Now write the function is_odd(n) that returns True when n is odd and False otherwise. Include doctests for this function as you write it. Finally, modify it so that it uses a call to is_even to determine

Re: [Tutor] flow problem with a exercise

2010-08-19 Thread Dave Angel
Roelof Wobben wrote: snip def is_odd(argument): uitkomst=is_even(argument) return uitkomst snip You forgot to indent the return statement to match the other statement(s) in the function. DaveA ___ Tutor maillist -

Re: [Tutor] List comprehension for dicts?

2010-08-19 Thread Emile van Sebille
On 8/19/2010 7:51 AM Pete said... Hi, I've been reading up on list comprehensions lately, all userful and powerful stuff - trying to wrap my brain around it :) As the examples all seem to relate to lists, I was wondering if there is an elegant similar way to apply a function to all keys in a

Re: [Tutor] List comprehension for dicts?

2010-08-19 Thread Pete
On 2010-08-19, at 5:25 PM, Emile van Sebille wrote: On 8/19/2010 7:51 AM Pete said... Hi, I've been reading up on list comprehensions lately, all userful and powerful stuff - trying to wrap my brain around it :) As the examples all seem to relate to lists, I was wondering if there is an

Re: [Tutor] List comprehension for dicts?

2010-08-19 Thread Steven D'Aprano
On Fri, 20 Aug 2010 09:51:08 am Pete wrote: [...] Ah, so list comprehensions are a purely syntactic construct? No, I don't think that's what Emile was trying to say. It's not like list comps are macros that are expanded into the explicit for-loop like that. All he is saying is that both the