[Tutor] Can I shorten this code?

Alan Gauld alan.gauld at btinternet.com
Sun Nov 20 00:18:01 CET 2011


On 19/11/11 19:01, Mic wrote:
> Hi!
> I am new to programming and it have recently come to my attention that
> alot of my code could be shortened.

Most peoples code could be shortened. And most of it would be the worse 
for it. There are a few occasions when shortening makes sense, 
especially if its all repeated lines or just plain superflous code.
But, in general, aim to write readable code, not just short code.

> from tkinter import*
>
> value1=("green")

You don't need parentheses here, they are doing nothing.

> click1=1

Rather than worrying about shortening try to think about more meaningful 
names for you variables. Why do they exist? Explain it in your names. In 
this case click1 is a flag to indicate the state
of button1. And what does button1 do? Name it to make it clear.

So a better name could be

buttonOneClicked = True  # Use True/False not 1/-1 to express boolean

> value2=("green")
> click2=1

Same here

buttonTwoClicked = True

> class Window(Frame):
> def create_widgets(self):
>
>     #Creates hello button1
>     self.hello_bttn1=Button(self,bg=value1)
>     self.hello_bttn1["text"]="Hi_1"
>     self.hello_bttn1["command"]=self.change_value_hellobttn1

You could do that all in one line:
      self.hello_bttn1=Button(self,  bg=value1,
                              text="Hi_1",
                              command=self.change_value_hellobttn1)

>     self.hello_bttn1.grid()
>
> #Creates hello button2
>     self.hello_bttn2=Button(self,bg=value2)
>     self.hello_bttn2["text"]="Hi_1"
>     self.hello_bttn2["command"]=self.change_value_hellobttn2
>     self.hello_bttn2.grid()

Same again

> def change_value_hellobttn1(self):
>
>      def change_click():
>         global click1
>         click1*=-1

If you use boolean values this becomes
           click1 = not click1

> change_click()
> if click1==-1:

and this becomes

    if click1

Or with a better name

    if buttonOneClicked:

> self.hello_bttn1.configure(bg="red")
> self.hello_bttn1.configure(text="Hi_2")

Again you can use a single line:

self.hello_bttn1.configure(bg="red", text="Hi_2")


> def change_global1():
> global value1
> value1=("red")

Again a bad name for the function. It changes value1 not global1...
Although I'm not sure what the purpose of these values are...

Also, although legal Python its usual to define all embedded functions 
in one place at the top of the containing function rather than inside
an if/else branch.

And even more commonly to define them as methods of the class rather 
than embedded functions.

> change_global1()
>
> elif click1==1:

Could just be an else since click1 is alwaysd either 1 or -1
Or buttonOneClicked  is always True or False using my suggestions.

> self.hello_bttn1["text"]="Hi_1"
> self.hello_bttn1.configure(bg="green")

Again you can use one line

self.hello_bttn1.configure(bg="green", text="Hi_1")


> def change_global2_1():
> global value1
> value1=("green")

And this is completely redundant since it does exactly the same as the 
other function, all you need is a parameter when you call it, like so:

def setValue1(val):
      global value1
      value1 = val

> change_global2_1()

And this becomes

setValue1("green")

But ultimately you could just have set it directly in your outer 
function, it's hardly worth the cost of defining a function, unless you 
intend to add more to it later.

> def change_value_hellobttn2(self):

All of the above comments apply here too.

> Imagine now that I want to do the same thing with 8 buttons, that I did
> to these two buttons. How would I shorten this?

Generally you build a data table with all the config parameters that 
will varty then you build a loop to read the values from the data table. 
Store the created buttons in a list or dictionary.

I'll leave that as an exercise :-)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list