Code Beauty Trumps that of the Display Output

 

Skin Deep or Deeper?

Don't get me wrong. Having a pretty output on your display is important.

However the fundamental point of using a higher level programming language is that its source code provides useful guidance to your brain (meaning to the brain of you as a code writer).

You want your code to be both self-descriptive and yet at the same time as simple as possible so that you can instantly understand what the basic goal of each code section is.

Here we enter the realm of Python Shallow Assignments and namespace aliases 

Say that you name your functions and variables as descriptively as possible by using very long names (VLN's). The reason for this is that in the future, merely reading the initially assigned name of that specific function or variable gives you a good hint as to what that function or variable is supposed to do, at least at a relatively high level.

In some of my code files, I have a function named qif_inStr_OK(arg1, arg2, arg3) where arg1 is named inStr and arg2 is a list named list_of_slices_2b_checked while arg3 is named list_of_allowed_substrings. Quite a mouthful. The prefix, "qif" in the function name is known by me to mean, Query If ... Thus, upon seeing the original assigned name, I know the function will receive an input string of some sort, it will receive a list of allowed or OK permutations and will return a True if the received input string is allowed according to the supplied list of allowed permutations.

It becomes cumbersome however, to deal with such very long names (VLN's) for functions, lists, modules or other associated objects. Enter the notion of name aliases, otherwise sometimes known as nick names.

The Python equal sign (=) generally does not invoke a deep copy but rather a "shallow assignment" in which a new object is NOT created. Rather the assigned new name (alias on the left side of the equal sign) is a pointer that merely points to the same object as did the name on the right side. So by using variable name aliases or function name aliases, we can make our source code simpler looking and easier to read (See also module aliases, class aliases, etc.)  See also, What is a deep copy?

Display Beauty versus Code Beauty

So below is an example of a console output whose colorful text substrings are generated using Colorama escape sequences:


However, the source code that generated the above is not cluttered with long name calls, for example to Colorama.Fore.CYAN. Instead there is a short alias call to {x2} in the below code example ...

intro_01c = 'Many other Python features are brought to light at the same time including but not limited to:'
intro_01c0 = f'(a) Using the {x3}COLORAMA{x2} module to print different colors on the console'
intro_01c1 = f'(b) Using {x3}string alignment{x2} (e.g., Str.center, spc:<10 ---="" a="" ack="" additional="" blog="" browser="" c="" d="" detect="" e="" ebster="" explore="" eyboard="" f="" functions="" g="" generate="" in="" indented="" info="" information="" informational="" inputs="" intro_01c2="f" intro_01c3="f" intro_01c4="f" intro_01c5="f" intro_01c6="f" keystroke="" like="" linking="" lists="" module="" more="" my="" of="" on-topic="" one="" open="" opening="" ore="" own="" page="" provide="" single="" stage="" the="" this="" to="" using="" web="" x0="" x2="" x3=""> {x3}Look in your browser -->{x2}'



In the above we see short named references like x0, x2, x3 repeated in curly braces. Earlier in the code we would also see shallow assignments that created them:
# COLOR and func call SHORTCUTS
x0 = cj('HWb')  #c0 = 'HWb';       #-- color combo shortcuts ... this one for BRIGHT WHITE on Black background
x1 = cj('Ncb')  #c1 = 'Ncb';       #-- Normal Cyan          ... it's short bc cj(cn) has no quote marks
x2 = cj('Dcb')  # c2 = 'Dcb';       #-- Dim Cyan
x3 = cj('y')    #c3 = 'y';         #-- Yellow
x4 = cj('NYb')


The cj() function stated on the right side of these shallow assignments is a translator that receives a string of length 1, 2 or 3 and returns calls to the Colorama Style, Fore and/or Back objects using the getaddr() method to link respective color declarations to these objects.
intro_01c_list = [intro_01c0, intro_01c1, intro_01c2, intro_01c3, intro_01c4, intro_01c5, intro_01c6 ]

def outp_list(list_name = intro_01c_list, colors = 'Ncb', indent=10):
    for item in list_name:
        print(f'{cj(colors)}{spc:&lt;10}{item}')       #-- note colon between spc and indent>
    #print(f'{x4}\nend of for loop hit #--a debug notification\n')
    return

outp_list()     #-- execute above func


The above for loop in the outp_list function generates the list of indented text lines

import copy
x = [[1], [2], [3]] #create a list (first layer) of lists (second layer)
y = x.copy() # make a shallow copy example which affects only the first layer
y = x.deepcopy() # make a deep copy example which creates a new second layer

Above code list is for next heading below re deep copies

Deep Copy

According to AnthonyWritesCode (here) a Deep Copy should be implemented if you want to create an actual separate copy of the second layer items (the lists inside the first layer item)

to be continued ???....

More to Explore
AnthonyWritesCode playlists
Anthony -- how does python's module `__getattr__` actually work?
Anthony --- python 3.14 highlights!

Comments

Popular posts from this blog

Links for Python Noobs

The Learn HOW to Learn Page

Welcome to Circular Import Hell