timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Python and Subroutines ?

Fri Aug 17, 2012 6:46 am

I'm used to keeping the primary code as short as possible and keeping all the tricky parts in subroutines .

Its very fast on processor power and makes the code a lot easier to maintain and debug.

But I'm very very new toPython and very unfamiliar with it.

Does Python have any thing similar ? And what is it called ? jargon for a new language is a struggling point for me.

Any thoughts very very welcome !

:D

Tim

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: Python and Subroutines ?

Fri Aug 17, 2012 6:55 am

Check out Functions and simply don't return any value to make a subroutine. Also check classes, a was of containing functions and data values, but don't go overboard with classes unless one becomes very useful, as they organize code, they also can bloat when your code is merged with other people code in a bigger project.

Look into Modules too, as they can be very useful.

Cheers Jacko
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Python and Subroutines ?

Fri Aug 17, 2012 7:12 am

I'll start looking at Modules, as it may be an easier stating point than functions !

Many many thanks , as I'm very nervous starting a new language !

:D :D :D

Tim

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Python and Subroutines ?

Fri Aug 17, 2012 7:21 am

@timmoore46: Maybe work through the tutorial in the Python documentation. Learning functions before modules makes more sense because modules are used to group functions (and other stuff).

Code: Select all

while not self.asleep():
    sheep += 1

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Python and Subroutines ?

Fri Aug 17, 2012 10:45 am

I'm sure that is wise advice, but this is what I tried earlier today. Many thanks !


Top Level Module 'Run' program
__
Import T1
Import T2
Print ("END)
__

Second level Module:- called 'T1'

# Print T1
print ("T1")
return

__

Second level Module:- called 'T2'

# Print T1
print ("T1")
return

__

Naturally it doesn't work in IDLE when I test RUN

but what have I missed ?

:D :)

Tim

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: Python and Subroutines ?

Fri Aug 17, 2012 10:51 am

T1.print vs. T2.print ? The module function must be "qualified" (uniquely identified may be a better expression, but it's not the word used).
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Python and Subroutines ?

Fri Aug 17, 2012 11:52 am

@jackokring: There is no `print` attribute in those modules.

@timmoore46: You are missing the error messages/exceptions you get with that code. There is no ``Import`` statetement and no `Print` function and ``return`` on module level raises a `SyntaxError`.

On top of that that is not the way modules should be used. There should be no code on module level but code defining constant attributes for the module object. Importing a module should not have side effects, otherwise the module concept becomes quite useless. Modules are meant to group constants, functions, and classes in a meaningful way, and to make those things reusable.

Code: Select all

while not self.asleep():
    sheep += 1

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: Python and Subroutines ?

Fri Aug 17, 2012 12:02 pm

You don't seem to be returning from within a function with a module, with your returns. I also think he has a name collision problem brewing. Function names can be used as references, and so I saw no error in function names being values/attributes. I thinks he's gunna need a def.

You've run into a "return not in missing def which being missing is not located in module error". But that's just typical :D

Cheers Jacko
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Python and Subroutines ?

Fri Aug 17, 2012 2:11 pm

Oh Dear !

I just want to write a program that can be used time and time again !

Namely turn on an external GPIO port and check that its working by reading an input.

(for as many ports that are available)

If I can get one pair of ports working then the rest should be identical apart from port numbers.

I'm sure Python can do it, but looks like I've started from the wrong end !

Does Python support labels and GOTO commands ?

:?

Many thanks everyone !

Any thoughts on best structure for that ?

:D

Tim

KCarscadden
Posts: 56
Joined: Sat Jun 09, 2012 9:00 pm
Location: Canada

Re: Python and Subroutines ?

Fri Aug 17, 2012 3:41 pm

You really should read some python tutorials or books. There are many on the internet for free.

However, here is a working example of what you are trying to do:

Code: Select all

#-Subroutines------------------------------------------------

def t1 ():

    print ("t1")
    return

def t2():

    print ("t2")
    return

#-Main-------------------------------------------------------

t1()
t2()

print("End")

You usually use separate modules to store reusable code, rather than just to arbitrarily make the code in a module smaller. But you could put t1 and t2 into one ot more separate modules.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Python and Subroutines ?

Fri Aug 17, 2012 3:56 pm

@timmoore46: Python has no labels and GOTOs, thank $GOD. Most modern languages don't have those, and even in languages which have they should not be used when there are other ways to structure the control flow.

I second KCarscadden here — work through a tutorial. There is one in the Python documentation. Another one for beginners is http://www.learnpythonthehardway.org/ (free HTML version on that website and don't get fooled by the title ;-)).

Code: Select all

while not self.asleep():
    sheep += 1

timmoore46
Posts: 266
Joined: Tue Jul 17, 2012 4:36 pm

Re: Python and Subroutines ?

Sat Aug 18, 2012 5:29 am

Many many thanks for that most useful info and sample program !

(too much machine code programming a million years ago ! *LOL* )

Very greatly appreciated !

:D :D :D :D :D :D

Tim

Return to “Python”