+ 33
[PythonEdu] How to create a module in Python?
Inspired by one of the threads in Q&A recently, I prepared a PythonEdu code on how to create own modules in Python to later be used (imported) in other programs. Of course, for building proper structures a whole lot of conventions and PEPs are applied. This is just an example on how simple it is to do. Have fun watching :) https://code.sololearn.com/cLF0P3C3eZ52/?ref=app https://www.sololearn.com/discuss/645722/?ref=app
13 Réponses
+ 16
It's THAT simple?
I thought Python modules were created with C!
+ 6
@David Ah, yes, the powerful dir() method. It lists all attributes of its argument (an object) or the list of names in the local scope, if called plain. Lets you know what can you do with this object, a bit.
+ 4
@00-00-72 may I ask how that contributes to the discussion here?
+ 2
@Kuba, I added these lines to your code:
import my_module
print(dir(my_module))
I expected to get:
['my_method']
but instead got:
['__builtins__', '__cached__', '__doc__', '__file__' '__loader__, '__name__', '__package__', '__spec__', 'my_method']
How cool is that! 😊
+ 2
@sayan chandra listdir() is a method of the os module so print(math.listdir()) will give an error.
+ 2
Srsly? That doesn't contribute much here?
We are talking about making modules and you are using a PSL module
+ 2
@00-00-72 Have you been able to import turtle into the SL code playground? When I've tried it, I get a "memory exceeded" message 😐
+ 2
Something like this?
https://code.sololearn.com/cV4DA87gTttG/?ref=app
+ 1
creating a simple module is like writing a sript and if you want to use it in interpreter you have import it using program name
+ 1
i don't know
0
very nice...
@ DAVID ASHTON
I TRIED THAT ALSO
AND GOT WHAT YOU SAID...
lokks like those builtins,cathed,name,etc
those are created by default if you creat a module.....
@ David//@ kuba
tell me...
suppose i imported math.....thennnnn
print(math.listdir())
and
print(dir(math))
are same right...?
- 1
import any module of Python from Python library file
- 4
hi