Reputation: 145
As a beginner I'm having a bit of trouble grasping importing in Python.
Say there is a module called testmodule.py such as this, which contains a class, method and another function:
class student:
def __init__ (self, n, a, s):
self.name = n
self.age = a
self.score = s
def ratio (self):
return self.age / self.score
def multiply (x):
return x * 10
Now I am importing that into my new script:
from testmodule import student
from testmodule import multiply
S1 = student("joe",33,98) #instance of the student Class defined in testmodule
S1.ratio() #method defined in the student Class defined in testmodule
multiply(10) #another function defined in testmodule, outside of the student Class
Where I am getting a bit lost is when I read other peoples code, I often have difficulty determining when and why certain items are being run as functions, vs methods, vs classes.
It seems to me, based on the example I have included, that despite using the same syntax of from X import Y, there is no clear differentiation at this point between whether what is being imported is a class or a function.
Thanks for any advice to help me get my head around this.
Upvotes: 0
Views: 44
Reputation: 61526
I often have difficulty determining when and why certain items are being run as functions, vs methods, vs classes.
Terminology: what you seem to mean by "items" are names, or identifiers. They aren't "run"; they refer to, or name a function, method, class, etc.
Fundamentally, when you import your own code as you showed, the reason that the imported student
refers to a class is that it was defined as such in the module you imported. So it is with everyone else's code: things are what they were defined to be.
But I assume the question you are actually trying to ask, is how you know whether the name refers to a function, class or something else. There are multiple possibilities:
You can read the documentation - that's what it's for.
You can inspect the code, if it's available.
You can rely on convention: we prefer to name classes LikeThis
, and functions like_this
. (You will know when something is a method, because you either had to access it as an attribute of a class: Student.ratio
or of an instance of that class: s1.ratio
.)
there is no clear differentiation at this point
Indeed. This is intentional (and I can't think of any other languages that force a distinction in a similar situation, either). In Python, we have things (objects) and names for those things (perhaps you have seen the exception called NameError
? It means exactly what it says); and everything is an object - including the classes themselves, and functions, and methods, and modules.
Upvotes: 3
Reputation: 1540
Well, generally class names should use CamelCase and start with uppercase according to PEP8.
Functions and variables should be snake case. e.g. do_this()
.
However, you are right, it is hard to tell whether something is being imported as a function or a class if they don't follow the correct naming conventions. It just gets easier with time, and hopefully whoever wrote the code used some descriptive names for their functions and classes.
If I see from module import student
, even though student isn't uppercase, chances are it is a class just because student is a noun. Vice versa, if I see from module import DownloadFile
, this is probably going to be a function despite the wrong naming convention.
Upvotes: 0
Reputation: 21
Normally the convention is to have the class names start with capital letter and functions with small letters. In your case, the student class should be defined as:-
class Student:
def __init__(self):
pass
Upvotes: 0