define methods in another file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: define methods in another file (/thread-7697.html) Pages:
1
2
|
define methods in another file - bb8 - Jan-21-2018 hi forum! i'm pretty new to python, i just wrote some simple codes in python. the examples i have seen and written define the method in the class declaration (which is also definition, i guess). i come from c++ background where the desired way for class methods is to declare the method in class's header file, and define it in class's source file. i wonder if i can do the same in python? RE: define methods in another file - ODIS - Jan-22-2018 Sure. You can do it with the import command: https://www.codementor.io/sheena/python-path-virtualenv-import-for-beginners-du107r3o1#the-import-mechanism-and-the-scope-and-whats-a-package-anyway RE: define methods in another file - buran - Jan-22-2018 well, if I understand your question correctly - in python there is no separate declaration and definition. RE: define methods in another file - Gribouillis - Jan-22-2018 (Jan-21-2018, 06:34 PM)bb8 Wrote: the desired way for class methods is to declare the method in class's header file, and define it in class's source file.In C++ there is a need for this: the header file allows one to compile independently code that uses the class and link it to the library at a later stage. In Python there is no such need. Bytecode can be produced for code that uses a class without knowing anything about the class. RE: define methods in another file - Python_n00b_06 - Feb-22-2018 If Python does not require the use of a header file, as in C++, then how would it work? Does each portion of the complete code have to be segregated by brackets, or can it just be written and the IDE or Python understand what you want and does it? RE: define methods in another file - Larz60+ - Feb-22-2018 Technically, you can write 'C' without a header file, but would get scoffed for it. RE: define methods in another file - Python_n00b_06 - Feb-22-2018 (Feb-22-2018, 06:31 PM)Python_n00b_06 Wrote: If Python does not require the use of a header file, as in C++, then how would it work? Does each portion of the complete code have to be segregated by brackets, or can it just be written and the IDE or Python understand what you want and does it?Should've checked grammar: If Python does not require the use of a header file, "unlike in C++", then how would it work? Does each portion of the complete code have to be segregated by brackets, or can it just be written and the IDE or Python understand what you want and does it? I am asking how would the code look without a header file in Python? Could someone link me a good example? RE: define methods in another file - Larz60+ - Feb-22-2018 Choose a package that interest you here: https://pypi.python.org/pypi download the source (there will usually be a link to the github repository on the introduction page for each package. Download or clone the source code and you can browse through each part of it. RE: define methods in another file - snippsat - Feb-22-2018 (Feb-22-2018, 10:12 PM)Larz60+ Wrote: or can it just be written and the IDE or Python understand what you want and does it?Yes if don't need stuff from standard library or 3-party module just write code and Python will understand it. secret_number = 34 tries, guess = 0, 0 while guess != secret_number: guess = int(input("Take a guess: ")) if guess > secret_number: print("Lower...") elif guess < secret_number: print("Higher...") tries += 1 print('You guessed it! The number was {} in {} tries'.format(guess, tries)) If file called guess.py run with python guess.py If as a example want use math from standard library,it's kind of similar to C++ example Python: from math import sqrt print(sqrt(4))C++: #include<iostream> #include<cmath> int main(){ cout<<sqrt(4); return 0; } The concept of import is kind of simple if look at a module.# foo.py def add(a, b): """This program adds two numbers and return the result.""" result = a + b return resultSo now i want to use add function in an other script(file). # bar.py from foo import add a = 11 b = 3 print(add(a, b)) # Use add from foo.py Package more stuff than just a couple of files and sharing stuff with wheel or PyPi,is a more complex,i have long tutorial about here RE: define methods in another file - Python_n00b_06 - Feb-23-2018 Ok i got some confusions figured out..i think: So basically "import" for Python is like "#include" for C/C++? |