Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

JulieNewbie

  • 3 years ago

What is the difference between find(target, key) and target.find(key)? They both seem to be syntactically correct in Python 2.7.2. However, neither produce output for me so I can't tell what difference it makes.

  • This Question is Closed
  1. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    hmm... the string module, which is part of the standard library, has a find method. to use this method you have to import the string module. Python has an str class to which all strings belong (everything in Python is an object - "abcd" is an str object). The str class is a built-in sequence data type. 'built-in' means that all of the str methods are available to any str object without having to import anything. built-ins are probably more efficient at 'doing their job'. in the idle 'shell' try dir('abcd') it will return all the attributes of the 'abcd' object http://docs.python.org/library/stdtypes.html#string-methods they both do the same thing, if you don't need any of the methods in the string module, you can use str methods and not have to import string. this is what i do, i don't know why - i have read a lot and decided to do things then forgotten what i read. :) you can actually open up standard library modules and see how they implemented things - they are .py files just like you have been writing. on my computer (windows) the string.py module is at C:\Python27\Lib. careful not to change anything. when i first saw your post i was confused by "find(target, key)" til i realized you must have used 'from string import *' - if you think you might end up using python a lot, have a look at these http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#importing http://docs.python.org/howto/doanddont.html#from-module-import

  2. JulieNewbie
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    So you're saying that find(target, key) is used when you have imported the string module, and target.find(key) is used if you haven't imported the string module, right? And they both do the same thing, which is find the first occurrence of a key in a target.

  3. carlsmith
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Not quite Julie. It's a little confusing at first: There's a built-in string method called find, so every string has a find method, you can call it on a string, or on a expression that evaluates to a string: >>>'abc'.find('a') 0 >>>word = 'apple' >>>word.find('ppl') 1 >>>('abc' + word).find('p') 4 >>> There is also a module in the Standard Library called string, which contains a function called find. If you do from string import * you'll now have a function called find in your namespace which works on strings. This is a classic example of why you should be cautious doing `from x import *`. I totally agree with bwCA, It's all a bit spurious in general use. I'm sure there's some reason for it, but in practice, at least while your starting out, don't import anything to do a simple find on a string, just use the built-in string method.

  4. JulieNewbie
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks- I'll stick to the basics without importing the string module.

  5. carlsmith
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    No worries, I've seen the question asked a few times. It does confuse people. I gave the wrong answer first time I saw it asked, and a couple of us ended up going back over it. I don't think there was a delete feature at that time. I think bwCA might have been there, so that might be why he remembers the answer, but not the details; same as me. But yeah, I just use the built-in string method, I've never come across a problem with it so far.

  6. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    thnx carlsmith that answer was a bit more coherent than mine

  7. JulieNewbie
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    bwCA- the "do and don't" documents were very interesting. Thanks.

  8. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    yw

  9. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.