0

I am new to programming therefore may sound idiotic. I am learning python where I am not able to understand how few methods like upper(), split() etc work.

I mean you directly use like below:

"ABC".upper()   or "abc,xyz".split(",")

Or, you can first import string and then call these methods like below:

import string
string.upper("abc") 

string.split("abc,xyz", ",")

What is the difference, and how would we import string module when we can achieve the same output without importing it.

Are there similar cases exist apart from string module?

2
  • 4
    You shouldn't import that module. It's provided only for backwards compatibility reasons, since the functions were moved to methods a long time ago. Commented May 24, 2016 at 7:26
  • 4
    As a new to programming and Python, you should be using Python 3 now. In Python 3, there is no string.split! Commented May 24, 2016 at 8:12

1 Answer 1

0

In fact, one of the paradigm you can use in Python is the Object Oriented Programming, where you modify object state through "methods" like this: myobject.mymethod().

Syntactically, it means that the first argument of the method mymethod() is in fact the object itself. But, as Python want also to deal with other paradigms (functional programming, imperative programming, and so on), there is two syntactical ways to address this method.

One is simply as I mentioned before: myobject.mymethod(), and the other one is simply to consider that the first argument is the object itself: mymethod(myobject).

More precisely, you can realize that when you define by yourself a method because you have to specify the first argument by self which is a reference to the object itself like this:

def mymethod(self):
    pass
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.