Python Forum
Custom function that prints function text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom function that prints function text
#1
Currently:

a = "abcde"
print("b[::-1] : ", b[::-1])
print("b[5:0:-1] : ", b[5:0:-1])
Output:
Output:
>>> b[::-1] : edcba >>> b[5:0:-1] : edcb
Can a custom function be defined that avoids duplicity in typing?
For ex:

def fuc(a)
    print(a, ":", a) # change here

fuc(b[::-1])
fuc(b[5:0:-1])
Output:
Output:
>>> b[::-1] : edcba >>> b[5:0:-1] : edcb
Reply
#2
Yes
Here's a dangerous way - you don't want to expose the eval() function to anything a user might enter for security purposes
def double_print(mystring) :
    print(mystring, eval(mystring))

a = "abcde"
double_print("a[::-1]")
Output:
a[::-1] edcba
Reply
#3
Maybe elaborate further what your actual goal is, because what you are doing/asking how to do is let say weird
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
I just mention that it will not work this way as b is not defined and you will get NameError anyway.

One can use f-strings debugging feature introduced in Python 3.8 for somewhat similar results ('=' instead of ':'):

>>> a = "abcde"
>>> print(f'{a[::-1]=}')
a[::-1]='edcba'
>>> print(f'{a[5:0:-1]=}')
a[5:0:-1]='edcb'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 644 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 576 Nov-23-2023, 02:53 PM
Last Post: rob101
  zfill prints extra et the end of a var tester_V 4 894 Mar-24-2023, 06:59 PM
Last Post: tester_V
  Writing into 2 text files from the same function paul18fr 4 1,671 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Doctesting a function which prints a students name along with the maximum mark scored sean1 5 2,268 Feb-01-2022, 12:20 PM
Last Post: Pedroski55
  Exit function from nested function based on user input Turtle 5 2,896 Oct-10-2021, 12:55 AM
Last Post: Turtle
  variable prints without being declared. ClockPillow 2 1,804 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  python prints none in function output chairmanme0wme0w 3 2,207 Jul-07-2021, 05:18 PM
Last Post: deanhystad
Question Stopping a parent function from a nested function? wallgraffiti 1 3,669 May-02-2021, 12:21 PM
Last Post: Gribouillis
Question exiting the outer function from the inner function banidjamali 3 3,522 Feb-27-2021, 09:47 AM
Last Post: banidjamali

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020