1

In sorted(list(mydict.keys())), sorted and list doesn't need an object prefix someobject., but keys() needed dict1.. When, or for what functions, is the prefix necessary?

1
  • 2
    You should think the other way around. Are objects that need .something() to call their methods. Commented Mar 18, 2012 at 16:07

2 Answers 2

6

Methods need to be called on a specific object. Functions don't.

The functions that are available at any time are the built-in ones, such as sorted and list, plus any functions that are in modules that you've imported or that you've defined yourself. The methods that are available on a particular object are the ones that are defined on that object's type.

Sign up to request clarification or add additional context in comments.

2 Comments

Do the built-in functions have their "implicit" objects?
No, actually, it's the other way around. All methods are really functions, and the object which the method is called on is implicitly passed as the first parameter. mydict.keys() is syntactic sugar for dict.keys(mydict).
2

The "prefix" means that you are calling a method from an object (someobject or dict in your example). If your function is not a method of an object, you do not need "a prefix"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.