Re: [Tutor] how to use replace() from a list
"Norman Khine" <[EMAIL PROTECTED]> wrote >name = title.lower().replace('/', '_').replace('?', > '_').replace('.', '') >return '_'.join(name.split()) > > Is there a way to have just one replace and so that: Check out the string.maketrans() function and the translate method. Basically you do something like: table = string.maketrans('/?_','___') title = title.translate(table) Now I'm not sure how it will react to the translation mapping all three chars to the same result char, but it should work... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to use replace() from a list
Hi I have this class: def title_to_name(title): title = title.encode('ascii', 'replace') name = title.lower().replace('/', '_').replace('?', '_').replace('.', '') return '_'.join(name.split()) Is there a way to have just one replace and so that: replace('/', '_').replace('?', '_').replace('.', '_') if I put these strings in a list remove = ['/', '.', '?'] I get this error: AttributeError: 'list' object has no attribute 'replace' Thanks Norman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Subclassing vs. stand alone functions
Thanks again, this is exactly the kind of info I need to make the jump from procedural to OO design. I bookmarked your site for reference. On Saturday, June 16, 2007, at 09:30AM, "Kent Johnson" <[EMAIL PROTECTED]> wrote: >[EMAIL PROTECTED] wrote: >> Hi there, >> >> I am new to Python and trying to get my head around the OO stuff. I >> guess my question is - when do you go with subclassing vs. making a >> standalone function? > >> Let's say you want to load a dictionary. Do I create a function that >> accepts some argument (say a file name) and returns a dictionary, or >> do I subclass dict and override the __init__ and __setitem__ >> functions to make 'self-loading' dictionary? It seems the end result >> is the same. > >I tend to reserve OOP for the cases where it provides a clear benefit >and use a procedural style otherwise. So in this case I would make a >separate factory function to load the dictionary because it is simpler. >Also you are not changing the behaviour of your new dict so overriding >seems unnecessary. (BTW Why would you override __setitem__?) > >There are many cases where OOP has a clear benefit in simper code, >encapsulation or reusability. There are times when you have to use OOP, >for example when using a library that is specialized by subclassing, >such as a GUI library or the cmd package. > >There are also many cases where OOP just adds unneeded complication to >your code; in these cases Python lets you write in the simpler >procedural style. > >I have written more on this topic here: >http://personalpages.tds.net/~kent37/stories/00014.html > >Kent > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using zip
lucio arteaga wrote: > I am trying to learn using zip in combination of if statemeny. , If I do > not make mistakes the program runs wll. Otherwise very bad? I don't understand your question. Are you talking about the zip() function or the zipfile module? What are you trying to do with it? Perhaps if you showed us the code you are having trouble with that would help. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Subclassing vs. stand alone functions
[EMAIL PROTECTED] wrote: > Hi there, > > I am new to Python and trying to get my head around the OO stuff. I > guess my question is - when do you go with subclassing vs. making a > standalone function? > Let's say you want to load a dictionary. Do I create a function that > accepts some argument (say a file name) and returns a dictionary, or > do I subclass dict and override the __init__ and __setitem__ > functions to make 'self-loading' dictionary? It seems the end result > is the same. I tend to reserve OOP for the cases where it provides a clear benefit and use a procedural style otherwise. So in this case I would make a separate factory function to load the dictionary because it is simpler. Also you are not changing the behaviour of your new dict so overriding seems unnecessary. (BTW Why would you override __setitem__?) There are many cases where OOP has a clear benefit in simper code, encapsulation or reusability. There are times when you have to use OOP, for example when using a library that is specialized by subclassing, such as a GUI library or the cmd package. There are also many cases where OOP just adds unneeded complication to your code; in these cases Python lets you write in the simpler procedural style. I have written more on this topic here: http://personalpages.tds.net/~kent37/stories/00014.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Subclassing vs. stand alone functions
<[EMAIL PROTECTED]> wrote >>Just so you know, my day gig is maintaining a 30 year old COBOL app >>and >>writing custom RPGLE - http://en.wikipedia.org/wiki/RPGLE - on an >>IBM i5. >>So that's where I am coming from. Thats probably one of the hardest places to learn OOP from. COBOL, more than any other language I've used forces programmers to separate data and function explicitly. It then compounds matters by encouraging the use of global variables (within modules at least). Of course COBOL is peerless in tackling exactly those problems where OOP is weakest - large volume data translation. But its a big jump from thinking in COBOL to thinking in OOP. I had to make the transition in the opposite direction and it was "challenging" Of course there is COBOL WITH OBJECTS now but I've no idea how that works. And with rigid discipline you can build with very small modules comprising precisely one data structure and the functions that operate on that but its not conventional COBOL practice. So I sympathise, but urge you to hang in there the penny will start to drop, especially if you try to create some projects that suit OOP - like building a few GUIs or simulations. Regards, Alan G. (2 years with MicroFocus COBOL on OS/360 for Y2K ;-) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor