How to use functions in separate files Perl and Python versions

Note: Functions and methods with _ at the beginning are private, so it is not good to access them from the outside. (Pycharm gives a warning)

Perl version

Foo.pm


package Foo;
use v5.10;
use strict;
use warnings;
use Exporter 'import';

our @EXPORT_OK = qw/pub_func/; # pub_Allow func functions to be exported

sub pub_func {
    say "pub_func";
}

sub _pri_func {
    say "_pri_func";
}
1;

main.pl


#!/usr/bin/perl
use v5.10;
use strict;
use warnings;

use FindBin qw($Bin);
use lib "$Bin";
use Foo qw/pub_func/; # pub_Import the func function into the namespace

sub main {
    pub_func();       # pub_func
    Foo::pub_func();  # pub_func
    Foo::_pri_func(); # _pri_func
}

if ($0 eq __FILE__) {
    main();
}

Python version

foo.py


# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, division, absolute_import


def pub_func():
    print("pub_func")


def _pri_func():
    print("_pri_func")

main.py


# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, division, absolute_import

import foo
from foo import pub_func  # pub_Import the func function into the namespace


def main():
    pub_func()      # pub_func
    foo.pub_func()  # pub_func
    foo._pri_func() # _pri_func
                    #Pycharm in my head_Functions with"Access to a protected member _pri_func of a module"Will give you a warning


if __name__ == '__main__':
    main()

For how to write from module import name1, name2 We do not recommend it because it may import multiple same function names into the namespace as follows.

Python good idioms, bad idioms

This time, it's a much weaker "no good" than the previous "no good", but it's still better to stop it unless there is a good reason. This usually doesn't work because you end up having one object that lives in two separate namespaces before you know it.

An improved version that doesn't import directly into the namespace

main2.pl


#!/usr/bin/perl
use v5.10;
use strict;
use warnings;

use FindBin qw($Bin);
use lib "$Bin";
#use Foo qw/pub_func/;
use Foo;

sub pub_func {
    return Foo::pub_func(@_);
}

sub main {
    pub_func();       # pub_func
    Foo::pub_func();  # pub_func
    Foo::_pri_func(); # _pri_func
}

if ($0 eq __FILE__) {
    main();
}

By doing this, the arguments and return values can be thrown in full, so the function name can be changed freely. I didn't know how to throw all the arguments in the Python version, so I omitted it. (I wonder if I have to write everything properly)

Recommended Posts

How to use functions in separate files Perl and Python versions
Comparison of how to use higher-order functions in Python 2 and 3
How to use is and == in Python
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Python] Summary of how to use split and join functions
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
python: How to use locals () and globals ()
How to switch python versions in cloud9
How to use __slots__ in Python class
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
How to use regular expressions in Python
How to use the C library in Python
How to generate permutations in Python and C ++
Summary of how to import files in Python 3
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
[Python] How to use hash function and tuple.
How to plot autocorrelation and partial autocorrelation in python
How to write the correct shebang in Perl, Python and Ruby scripts
[For beginners] How to use say command in python!
python3: How to use bottle (2)
Tips for those who are wondering how to use is and == in Python
Include and use external Kv files in Python Kivy
How to use Python argparse
Python: How to use pydub
[Python] How to sort dict in list and instance in list
[Python] How to use checkio
How to use variables in systemd Unit definition files
How to download files from Selenium in Python in Chrome
How to add page numbers to PDF files (in Python)
[Python] How to split and modularize files (simple, example)
I tried to summarize how to use pandas in python
How to use the model learned in Lobe in Python
How to use Decorator in Django and how to make it
How to develop in Python
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to install and use pyenv, what to do if you can't switch python versions
Notes on how to use StatsModels that can use linear regression and GLM in python
How to swap elements in an array in Python, and how to reverse an array.
How to use the __call__ method in a Python class
How to create and use static / dynamic libraries in C
[Python] Use this to read and write wav files [wavio]
How to execute external shell scripts and commands in python
How to log in to AtCoder with Python and submit automatically
[Python] How to do PCA in Python
How to install and use Tesseract-OCR
How to use classes in Theano
[Python] How to use Pandas Series