Script Inheritance Questions

This is the place for queries that don't fit in any of the other categories.

Script Inheritance Questions

Postby DevynCJohnson » Sun May 26, 2013 7:24 pm

I have a Python3.x script called Parent.py, and some functions and variables are set in the script. Parent.py executes a Python3.x script called Child.py. Will Child.py be able to use the functions set in Parent.py? Can Child.py also access the variables set in Parent.py? Would it make a difference if the variables were made global?

Code: Select all
global SomeVariable = 'STRING'


If variable values are changed in Child.py, will the values be changed in Parent.py?
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Script Inheritance Questions

Postby stranac » Sun May 26, 2013 7:47 pm

It sounds like you're doing something weird.
You should post the code you currently have, maybe that will make more sense.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: Script Inheritance Questions

Postby DevynCJohnson » Sun May 26, 2013 8:02 pm

Each script is over one thousand lines of code. The parent script actually executes three child scripts. I want to know if the child scripts will use the variables and functions created in the parent script. For this project that I am working on, I am manually converting BASH code into Python3.x code. I know a lot about Python3.x, but many topics I am not sure about.
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Script Inheritance Questions

Postby stranac » Sun May 26, 2013 8:06 pm

DevynCJohnson wrote:I want to know if the child scripts will use the variables and functions created in the parent script.

If I understand correctly what you're doing, the answer is no.
But, in case I'm misunderstanding something, it should be very simple for you to test with a simplified example.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: Script Inheritance Questions

Postby DevynCJohnson » Sun May 26, 2013 8:23 pm

I made two small test scripts (seen below). I get errors when Parent.py is executed from a terminal. Now I need to find a way to make this work. I need a way to make the child scripts inherit the variables and functions of the parent.
While I was performing this experiment, I also tried to see if the scripts inherit imported modules. Child scripts do not inherit imported modules either. (I do not need to do this though)

Parent:

Code: Select all
#!/usr/bin/python3
#Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS
import subprocess
VAR = 'FROM PARENT'
subprocess.Popen('./child.py')


Child:

Code: Select all
#!/usr/bin/python3
#Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS
subprocess.getoutput('date')
import io, sys
file = open('./test.TXT', 'w'); file.write(VAR); file.close()
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Script Inheritance Questions

Postby stranac » Sun May 26, 2013 8:33 pm

Yeah, this is just not something you're supposed to be doing at all.

That's not how you make multiple python files work together.
You could probably make this work with some exec() hackiness, but it's just not worth it.

Your child scripts should probably be modules that your parent script imports, and calls some functions in them.
If you need a module in your child scripts, you should import it there.
What you're trying to do now doesn't make much sense.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: Script Inheritance Questions

Postby snippsat » Mon May 27, 2013 2:17 am

As stranac mention do it the normal way by using import.
Dont use file as variable name,because it's the name of a builtin function.
Code: Select all
#parent.py
var = 'FROM PARENT'

Code: Select all
#child.py
from parent import var

with open('test.txt', 'w') as f_out:
    f_out.write(var) #-->FROM PARENT
User avatar
snippsat
 
Posts: 233
Joined: Thu Feb 21, 2013 12:04 am

Re: Script Inheritance Questions

Postby DevynCJohnson » Mon May 27, 2013 12:13 pm

Snippsat, is there a way I can import all variables and functions from the parent without typing each one? The parent script contains over one hundred variables that it creates. Thank you for the suggestion of importing; I always thought imports only worked with modules.
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Script Inheritance Questions

Postby Mekire » Mon May 27, 2013 12:19 pm

You absolutely can; and absolutely shouldn't.

The syntax is:
Code: Select all
from my_module import *

Please never use it :)

What you should do instead is:
Code: Select all
import my_module
and then access your variables by prefixing them as in:
Code: Select all
my_module.my_var

If you think it is too much effort to prefix the vars every time, you can shorten the prefix to something else as in:
Code: Select all
import my_module as mine
where after you could access your variable with:
Code: Select all
mine.my_var

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Script Inheritance Questions

Postby DevynCJohnson » Mon May 27, 2013 12:46 pm

Mekire, what is wrong with "from parent.py import *"? It seems like the perfect solution.
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Script Inheritance Questions

Postby stranac » Mon May 27, 2013 1:14 pm

There are a number of reasons star imports are bad:
  • they throw a bunch of names in your namespace
  • they can cause name collisions
  • they're unreadable(no way of telling what was imported, and where a certain name comes from)
Just use import module instead, and use fully qualified names to access your stuff.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: Script Inheritance Questions

Postby DevynCJohnson » Mon May 27, 2013 1:40 pm

Thank you everyone. The command "from parent.py import *" is the command that I am looking for. I am very careful about variable names, so the negative possibilities of the command do not pertain to me.
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Script Inheritance Questions

Postby Mekire » Mon May 27, 2013 2:15 pm

No offense, but that is an extremely naive (not to mention incorrect) viewpoint. The case where you are importing hundreds of names is EXACTLY the case that you don't want to do this with. The only case it might be remotely acceptable is if there were only very few things and even then they should follow very strict name conventions. The question is not whether or not you can follow your logic. The question is if others can follow your logic or even if your future self can follow your logic. The fact of the matter is there is no way to tell in a sizable script where a name comes from if you do this.

Many new python programmers see star imports and think just as you do. They are all wrong. You are being told specifically by those more experienced in the language not to do this. You might consider that there is a greater reason than us merely being pedantic.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Script Inheritance Questions

Postby metulburr » Mon May 27, 2013 3:06 pm

I am very careful about variable names, so the negative possibilities of the command do not pertain to me.


This viewpoint will one day get you into trouble. Then at that point you will be so use to using * that it will take you a LONG time to re-adjust. Why start out the wrong way? Why not take experienced programmers advise? I can assure you that flooding the namespace is always a bad idea. I know there are a ton of examples out there that use this, but its wrong.

At first glance of seeing someone use the *, the first thing i think of is a newbie is asking a question. Most likely that code will also contain global keywords too. I just outright shows that its newbie code written. Sometimes i skip people's questions that their code contain this syntax, as at those times, i dont feel like re-explaining what everyone here has already explained to you. So tkae everyone's advice, as you may also get more responses when not using this syntax. One day you will know, and be telling someone new one here the same thing we are telling you.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Script Inheritance Questions

Postby micseydel » Mon May 27, 2013 10:43 pm

Mekire wrote:The question is if others can follow your logic or even if your future self can follow your logic.

This is the biggest point for me here, especially with respect to others. If you say "well this code is just for me!" then you shouldn't be posting on a forum, and there are other good reasons too. Do you intend to ever get a job programming? You should have good sample code to show for it (and good habits for that matter). You don't? Ok, do you want to share your work with others, possibly allowing them to improve on your creation? (This is a very common thing in open source software, and it's quite a compliment when someone does it. Also, the fact that you're including your name in your script implies this to me.)

Again there's the whole "learn from your elders" thing. I didn't appreciate that much before becoming a more seasoned programmer. If you ignore it, that's on you, but just as metulburr said there are people who might skip over your question because of what a mess your code is. But really, I would take to heart that quote that goes "Smart people learn from their mistakes. Wise people learn from others' mistakes." (Paraphrased from Brandon Mull.)

The closest thing to ignoring the wisdom of more knowledgeable programmers that I've really been bit by is with regard to object oriented programming (OOP). I wanted to just "use classes" because they are an advanced feature in programming languages. But they're a tool to implement OOP and some design patterns, not something which is stand-alone like functions. So I learned the syntax of classes without learning OOP, and I wrote some nasty code. I didn't know what I was doing, it was frustrating, and that particular script, while I consider it to be one of the most useful things I've ever written, I don't want to share it with others because it's embarrassing frankly. If I'd taken the time to properly learn OOP, I would have had something wonderful to show for my effort. For you now the only time investment is whatever it takes to adjust your mindset, which is hopefully not much (and will be more the longer you wait!).
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1391
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests