Re: [Tutor] class method problem
I looked up the word zoeken -- it means find in English. I looked up the chapter and exercise in the online text "How to think like a computer scientist" Its a good tutorial. I think the OP seems to get confused on basic concepts. Here is my stab at the exercise: #!/usr/bin/env python """This exercise is to take the find function, alter it so that it can specify an end point for the search. It is problem 2 at the end of this chapter: http://openbookproject.net/thinkcs/python/english2e/ch15.html The author notes that end should default to len(str), but it can't since default parameters are initialized when the function is defined and not when it is invoked. """ # this is from the author's text def find(str, ch, start=0): index = start while index < len(str): if str[index] == ch: return index index = index + 1 return -1 # this I believe is the concept the author was looking to get the reader to understand def find2(str, ch, start=0, end = None): if end == None: end = len(str) index = start while index < end: if str[index] == ch: return index index = index + 1 return -1 -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class method problem
On 2:59 PM, Roelof Wobben wrote: Hello, Fine that you are in a arque But can we come back to my problem. How can I take care that test2 can be initialized. Roelof You had this code, and got the following error: class zoeken() : pass def __len__(self): return 0 def __str__(self): return test2 def find(self, strng, ch, start, stop): index = start while index< len(strng) and index< stop: if strng[index] == ch: return index index += 1 return -1 test = zoeken() test.woord = "tamara" test2 = zoeken.find(test, "a", 1,5) print test(test2) But now I get this message : Traceback (most recent call last): File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 20, in test2 = zoeken.find(test, "a", 1,5) TypeError: find() takes exactly 5 arguments (4 given) The immediate answer is that 1) you are calling the find method with the class as the first argument, while it was declared with 'self'. 2) you never pass a string to be searched, so there's nothing useful to be passed to strng. It's the second problem that causes the error, but the first one is tied with it. You can fix the immediate syntax error by passing the string explicitly. test2 = test.find("tamara", "a", 1, 5) The real problem though is one of design, or intent. You're assigning that "tamara" to an attribute of a particular instance of zoeken(). So did you mean that find() would look there for it? If that were the case, the find declaration should change to: def find(self, ch, start, stop): and naturally, the reference to strng[index] should change to self.woord[index] There are several things wrong with the find() method, and with the __str__() method, but the first question that needs answering is What does a zoeken (please capitalize it, to follow Python conventions) object represent? What are its data attributes going to mean? Since it's not English, I don't get a clue from the name, but normally I'd get one from the __init__() method, which you omitted. My convention is to initialize all attributes in the __init__() method, even if they're not always used. But if that's undesirable for some reason, then at least comment on them there. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class method problem
Hello, Fine that you are in a arque But can we come back to my problem. How can I take care that test2 can be initialized. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class method problem
On Sun, Sep 26, 2010 at 3:14 AM, Steven D'Aprano wrote: > On Sun, 26 Sep 2010 02:26:25 pm David Hutto wrote: >> On Sat, Sep 25, 2010 at 9:16 PM, Steven D'Aprano > wrote: >> > On Sun, 26 Sep 2010 08:13:23 am David Hutto wrote: >> >> Since I had nothing else to do, but practice, this looks much >> >> better: >> >> >> >> def find(word, search): >> >> if search in word: >> >> print True >> >> else: >> >> print False > [...] >> OP wanted to find if a in b. Does my function do that?...Yep > > I didn't say that the function did the wrong thing, I said the name was > misleading for what it actually did. > > Why didn't you call the function len(), or deleteItemsFromPage()? > Because those names would be misleading, deceptive and silly. And so is > calling a function find() when it does something completely different > from the commonly understood meaning of the word "find". > > When you find something, you know where it is afterwards. Find to me, in the since of the OP's question means a hit. It was found in the furtherence into where, is up to the OP and their next question on the list that says now that I know it's there, where is it? In the context > of text searches, that means returning an offset to where the target is > found, or something similar. That's what str.find() does, and regular > expressions, and list.index(). Your "find" function is a membership > test, which is what the `in` operator is for. And I'm sure that with google, and my trust Building Skills in Python Manual at my side, I would have found those string/list functions, and overtime so will the OP, just as did at one point Mr. Miyagi. > > > > >> > Yes, we found your terms on the Internet, but we won't tell you >> > where. If you would like to find something else, we won't tell >> > you where that is either. >> >> It's not supposed to be a four line version of Google. > > I didn't say it was. I tried to inject a little humour into the email, > but it clearly didn't work. It did work, so I injected a little sarcasm back. > > > [...] >> > Even this is slightly misleading, because "text" doesn't need to be >> > an actual string. It could be any sequence, such as a list. I know this from a brief exercise I was working on before, but when learning, you can just give everything at once and expect it to be absorbed, you have to practive with the different types you can manipulate, but knowing what they are is helpful. But >> > this gives the *intention* of the function, which is to do text >> > searches. So this is (in my opinion) an acceptable compromise >> > between intention and generality. >> >> Semantics, are only important to those who didn't write it. > > "Semantics" means "meaning". If you are trying to tell me that the > meaning of programs -- their *purpose* -- is unimportant, that it > doesn't matter what a function does, I'm afraid you'll have your work > cut out to convince me. I meant if it's a small function that you might use once in a blue moon, and you know what it does for you, and since you wrote this small function, you're able to tell exactly what it does, and then it does this task as expected for you, then ok. If I was working with someone else on it, then I'm sure, they would suggest, that we use overlapping terminology we could agree on for the code at hand. (it would probably require a meeting or something.) > > The most important reader of your functions is... you. The function > might be fresh in your mind *today*, but tomorrow? Next week? Six > months from now when you discover a bug in your program and are trying > to remember how it works so you can fix it? It's five lines long. > > Trust me, any non-trivial program that you don't just use once and throw > away needs to be written with a careful eye to naming functions and > arguments. The ideal is that you should never need to write > documentation, because the functions and arguments document themselves. > (This is an ideal that never quite works in practice, but we can at > least *try*.) and I do. > > An excellent piece of advice I've been given is to write your program's > API -- the names of functions, the calling signatures, and so forth -- > as if the next person to maintain that code will be a psychopath with a > hair-trigger temper and a gun and who knows where you live. Since the > next person to maintain it will likely be you, you will save *much* > more time in the future by careful and consistent naming than it costs > to think of those names right now. Your comments are in the suggestion box, and you'll be happy to know, I even take advice from my critics...when it suits me. > > > > -- > Steven D'Aprano > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > David Hutto ___ Tutor maillist - Tutor@python.org
Re: [Tutor] class method problem
On Sun, 26 Sep 2010 02:26:25 pm David Hutto wrote: > On Sat, Sep 25, 2010 at 9:16 PM, Steven D'Aprano wrote: > > On Sun, 26 Sep 2010 08:13:23 am David Hutto wrote: > >> Since I had nothing else to do, but practice, this looks much > >> better: > >> > >> def find(word, search): > >> if search in word: > >> print True > >> else: > >> print False [...] > OP wanted to find if a in b. Does my function do that?...Yep I didn't say that the function did the wrong thing, I said the name was misleading for what it actually did. Why didn't you call the function len(), or deleteItemsFromPage()? Because those names would be misleading, deceptive and silly. And so is calling a function find() when it does something completely different from the commonly understood meaning of the word "find". When you find something, you know where it is afterwards. In the context of text searches, that means returning an offset to where the target is found, or something similar. That's what str.find() does, and regular expressions, and list.index(). Your "find" function is a membership test, which is what the `in` operator is for. > > Yes, we found your terms on the Internet, but we won't tell you > > where. If you would like to find something else, we won't tell > > you where that is either. > > It's not supposed to be a four line version of Google. I didn't say it was. I tried to inject a little humour into the email, but it clearly didn't work. [...] > > Even this is slightly misleading, because "text" doesn't need to be > > an actual string. It could be any sequence, such as a list. But > > this gives the *intention* of the function, which is to do text > > searches. So this is (in my opinion) an acceptable compromise > > between intention and generality. > > Semantics, are only important to those who didn't write it. "Semantics" means "meaning". If you are trying to tell me that the meaning of programs -- their *purpose* -- is unimportant, that it doesn't matter what a function does, I'm afraid you'll have your work cut out to convince me. The most important reader of your functions is... you. The function might be fresh in your mind *today*, but tomorrow? Next week? Six months from now when you discover a bug in your program and are trying to remember how it works so you can fix it? Trust me, any non-trivial program that you don't just use once and throw away needs to be written with a careful eye to naming functions and arguments. The ideal is that you should never need to write documentation, because the functions and arguments document themselves. (This is an ideal that never quite works in practice, but we can at least *try*.) An excellent piece of advice I've been given is to write your program's API -- the names of functions, the calling signatures, and so forth -- as if the next person to maintain that code will be a psychopath with a hair-trigger temper and a gun and who knows where you live. Since the next person to maintain it will likely be you, you will save *much* more time in the future by careful and consistent naming than it costs to think of those names right now. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class method problem
On Sat, Sep 25, 2010 at 9:16 PM, Steven D'Aprano wrote: > On Sun, 26 Sep 2010 08:13:23 am David Hutto wrote: >> Since I had nothing else to do, but practice, this looks much better: >> >> def find(word, search): >> if search in word: >> print True >> else: >> print False > > > For some definition of "better". > > If I called a function: > > find("anti-disestablishmentarianism", "lish") > > and got back an answer: > > True > > I'd feel ripped off and cheated. That would be like going to Google, > typing in something into the search box, and Google comes back with: OP wanted to find if a in b. Does my function do that?...Yep > > Yes, we found your terms on the Internet, but we won't tell you > where. If you would like to find something else, we won't tell > you where that is either. It's not supposed to be a four line version of Google. > > Aside from the name of the function, which is deceptive because it > doesn't describe what the function does, the names of the arguments are > also poor. The first argument is not necessarily a word. Badly named yes, but I know it just finds a string in a string Nor is there > any need for it to be -- it can be any text. The second argument is > poorly described as "search" -- search is a verb. > > A better function signature might be: > > def search(text, target): > # Search for target in text and print whether it is found or not. > > Even this is slightly misleading, because "text" doesn't need to be an > actual string. It could be any sequence, such as a list. But this gives > the *intention* of the function, which is to do text searches. So this > is (in my opinion) an acceptable compromise between intention and > generality. Semantics, are only important to those who didn't write it. > > Now on to the code itself. The body of the function is needlessly > verbose. You say: > > if search in word: > print True > else: > print False > > This is so simple we can trace the entire function by hand. Say we call > search("I like spam and eggs", "spam"): > > (1) target in text? => True > (2) take the if branch > (3) print True > > Now say we call find("I like spam and eggs", "cheese"): > > (1) target in text? => False > (2) take the else branch > (3) print False > > Can you see the common factor? The object which is printed is always > precisely the same object generated by the `in` test. So we can > simplify the body of the function: > > def search(text, target): > print target in text > > > But this is so simple, there's no point to wrapping it in a function! > > Small functions that do one thing are good, up to a point, but when the > function is so small that it is just as easy to include the body in the > caller code, the function is pointless. It's not like "search(b, a)" is > easier to write or remember than "print a in b" -- if anything the > opposite is the case, because I would never remember which order to > pass the arguments. The reason I put it in a function, was the same reason the OP put it in a class, to eventually expand on the initial program being created.(also I had it in a snippets file, accompanied by an instance for it, a way to conveniently call it later if needed) I do know I could have shortened it further, I'm not all omniscient like yourself. Breath deeply, and remember it's someone elses code who hasn't had the same experience writing code that you have. But thanks for pointing it out, I'll know to eliminate the excess in the future, knowing that the all seeing steven is watching my every function. > > > > -- > Steven D'Aprano > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class method problem
On Sun, 26 Sep 2010 08:13:23 am David Hutto wrote: > Since I had nothing else to do, but practice, this looks much better: > > def find(word, search): > if search in word: > print True > else: > print False For some definition of "better". If I called a function: find("anti-disestablishmentarianism", "lish") and got back an answer: True I'd feel ripped off and cheated. That would be like going to Google, typing in something into the search box, and Google comes back with: Yes, we found your terms on the Internet, but we won't tell you where. If you would like to find something else, we won't tell you where that is either. Aside from the name of the function, which is deceptive because it doesn't describe what the function does, the names of the arguments are also poor. The first argument is not necessarily a word. Nor is there any need for it to be -- it can be any text. The second argument is poorly described as "search" -- search is a verb. A better function signature might be: def search(text, target): # Search for target in text and print whether it is found or not. Even this is slightly misleading, because "text" doesn't need to be an actual string. It could be any sequence, such as a list. But this gives the *intention* of the function, which is to do text searches. So this is (in my opinion) an acceptable compromise between intention and generality. Now on to the code itself. The body of the function is needlessly verbose. You say: if search in word: print True else: print False This is so simple we can trace the entire function by hand. Say we call search("I like spam and eggs", "spam"): (1) target in text? => True (2) take the if branch (3) print True Now say we call find("I like spam and eggs", "cheese"): (1) target in text? => False (2) take the else branch (3) print False Can you see the common factor? The object which is printed is always precisely the same object generated by the `in` test. So we can simplify the body of the function: def search(text, target): print target in text But this is so simple, there's no point to wrapping it in a function! Small functions that do one thing are good, up to a point, but when the function is so small that it is just as easy to include the body in the caller code, the function is pointless. It's not like "search(b, a)" is easier to write or remember than "print a in b" -- if anything the opposite is the case, because I would never remember which order to pass the arguments. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class method problem
On Sun, 26 Sep 2010 04:15:03 am Roelof Wobben wrote: > Hello, > > I have this code: > > class zoeken() : It is traditional to name classes with an initial capital letter, so Zoeken would be better. > pass What is the point of the "pass" statement there? That does nothing. Why did you put that there? > def __len__(self): > return 0 > def __str__(self): > return test2 What is test2? It doesn't exist. > def find(self, strng, ch, start, stop): Count the arguments: 5, including self. Remember that number. This is important later on. > index = start > while index < len(strng) and index < stop: > if strng[index] == ch: > return index > index += 1 > return -1 Watch the indentation. The "return -1" is *inside* the loop. > test = zoeken() > test.woord = "tamara" > test2 = zoeken.find(test, "a", 1,5) > print test(test2) > > But now I get this message : > > Traceback (most recent call last): > File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 20, > in test2 = zoeken.find(test, "a", 1,5) > TypeError: find() takes exactly 5 arguments (4 given) Right. READ THE ERROR, don't just immediately cry for help. Being a programmer means you must have ATTENTION TO DETAIL -- the error tells you *exactly* what the problem is: the find() method takes five arguments, *including* self. You have only given four arguments: find method expects: 1: self 2: strng 3: ch 4: start 5: stop find method actually gets 1: test 2: "a" 3: 1 4: 5 5: ?? > I can do zoeken.find (test2,test, "a", 1,5) but then I get this > message: > > Traceback (most recent call last): > File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 20, > in zoeken.find( test2, test, "a", 1,5) > NameError: name 'test2' is not defined Exactly. That's because test2 does not exist. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class method problem
Since I had nothing else to do, but practice, this looks much better: def find(word, search): if search in word: print True else: print False word = raw_input('Enter string of letters to search: ' ) search = raw_input('Enter character to find: ') find(word,search) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class method problem
This is a little better, it returns a if "a" in, or None if using "z": class zoeken() : pass def __len__(self): return 0 def __str__(self): return test2 def find(self, strng, ch, start, stop): index = start while index < len(strng) and index < stop: if strng[index] == ch: return ch index += 1 test = zoeken() test.woord = raw_input('Enter string of letters to search: ' ) stop = len(test.woord) search = raw_input('Enter character to find: ') #print 'search =' ,search test2 = test.find(test.woord, search, 1,stop) print test2 Of course, there are other ways to accomplish this. David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class method problem
This returns a if "a" in, or -1 if using "z": class zoeken() : pass def __len__(self): return 0 def __str__(self): return test2 def find(self, strng, ch, start, stop): index = start while index < len(strng) and index < stop: if strng[index] == ch: return ch index += 1 return -1 test = zoeken() test.woord = "tamara" stop = len(test.woord) test2 = test.find(test.woord, "a", 1,stop) print test2 David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] class method problem
Hello, Still the same errors Roelof > >> Date: Sat, 25 Sep 2010 19:33:52 +0100 >> Subject: Re: [Tutor] class method problem >> From: andre...@gmail.com >> To: rwob...@hotmail.com >> >> Your method receives 4 arguments and you didn't define one default. >> Try to do something like this: >> >> def find(self, strng=None, ch=None, start=None, stop=None): >> >> Or any other default values that matches your needs. >> >> >> >> On 25 September 2010 19:15, Roelof Wobben wrote: >>> >>> >>> Hello, >>> >>> I have this code: >>> >>> class zoeken() : >>> á ápass >>> á ádef __len__(self): >>> á á á áreturn 0 >>> á ádef __str__(self): >>> á á á áreturn test2 >>> á ádef find(self, strng, ch, start, stop): >>> á á á áindex = start >>> á á á áwhile index < len(strng) and index < stop: >>> á á á á á áif strng[index] == ch: >>> á á á á á á á áreturn index >>> á á á á á áindex += 1 >>> á á á á á áreturn -1 >>> >>> >>> test = zoeken() >>> test.woord = "tamara" >>> test2 = zoeken.find(test, "a", 1,5) >>> print test(test2) >>> >>> But now I get this message : >>> >>> Traceback (most recent call last): >>> áFile "C:\Users\wobben\workspace\oefeningen\src\test.py", line 20, in >>> á átest2 = zoeken.find(test, "a", 1,5) >>> TypeError: find() takes exactly 5 arguments (4 given) >>> >>> I can do zoeken.find (test2,test, "a", 1,5) but then I get this message: >>> >>> Traceback (most recent call last): >>> áFile "C:\Users\wobben\workspace\oefeningen\src\test.py", line 20, in >>> á ázoeken.find( test2, test, "a", 1,5) >>> NameError: name 'test2' is not defined >>> >>> >>> Roelof >>> >>> ___ >>> Tutor maillist á- átu...@python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >> >> >> >> -- >> Andreh Palma ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] class method problem
Hello, I have this code: class zoeken() : pass def __len__(self): return 0 def __str__(self): return test2 def find(self, strng, ch, start, stop): index = start while index < len(strng) and index < stop: if strng[index] == ch: return index index += 1 return -1 test = zoeken() test.woord = "tamara" test2 = zoeken.find(test, "a", 1,5) print test(test2) But now I get this message : Traceback (most recent call last): File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 20, in test2 = zoeken.find(test, "a", 1,5) TypeError: find() takes exactly 5 arguments (4 given) I can do zoeken.find (test2,test, "a", 1,5) but then I get this message: Traceback (most recent call last): File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 20, in zoeken.find( test2, test, "a", 1,5) NameError: name 'test2' is not defined Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor