-4

How else can I implement a function without using ' '.join(str, name). I use this to convert tuple to str. And function def get_text should look:

def get_text(name):
    return "Hello " + name

Is there another way to implement that?

def bold(get_text):
    def wrapped(*name):
        return "<b>{}</b>".format(get_text(" ".join(map(str, name))))
    return wrapped

def italic(get_text):
    def wrapped(name):
        return "<i>{}</i>".format(get_text(name))
    return wrapped

def underline(get_text):
    def wrapped(name):
        return "<u>{}</u>".format(get_text(name))
    return wrapped

@bold
@italic
@underline

def get_text(*name):
    return "hello " + " ".join(map(str, name))


print(get_text('Hi', 'world'))

Function returns:

<b><i><u>hello Hi world</u></i></b>
  • Why do you want to avoid `' '.join`? – chepner Jul 05 '22 at 12:16
  • I avoid ' '.join cause my tutor asked for me a find another way to relize that. But I can't solve this. Cause arguments are returned in tuple and i can't concatenate 'Hello ' + name, cause name is tuple – Lukky Bakky Jul 05 '22 at 12:21
  • buran, what do u mean? – Lukky Bakky Jul 05 '22 at 12:22
  • You've shown `text` and `get_text`, two functions which do different things. What is your function actually supposed to do? (Also I don't see how `bold`, `italic`, or `underline` are relevant to your question.) Taking the [tour] and reading [ask] are good places to start here. – CrazyChucky Jul 05 '22 at 12:27
  • Okay, now they're both named `get_text`, but they still do different things. What is your ultimate goal for this function? What should it accept, and what should it return? We can't begin to help you do something if we don't know what that thing is. – CrazyChucky Jul 05 '22 at 12:31
  • Function get_text should accept arguments(str). So, after that this arguments should be wrapped by tags. For example, function get_text accepts "Hi", "World" and this arguments should warapped by , , . It must be returned hello Hi world – Lukky Bakky Jul 05 '22 at 12:39
  • Huh. In that case, I can't understand why your tutor would forbid you from using the simplest and best method to combine an arbitrary number of strings. – CrazyChucky Jul 05 '22 at 12:41
  • There are 2 things either slash or forward slash. (1)return "{}".format(get_text(name)) and (2) return "{}<\u0332">".format(get_text(name)) – toyota Supra Jul 05 '22 at 12:59

1 Answers1

-1

First of all, you try to re-invent wheel. If you want to make html/xml etc. page from python - there are already libraries/frameworks and tutorials for this, that will suit you much better. However, if it's just he beginning of your programming journey, string manipulation is one of first steps.... however you also have tons of tutorials for this. Python has multiple methods to format strings (best depends on usage, really)... e.g.

Basically, string documentation is where you want to look (also, tutorials above will do the trick as well)

What you are attempting to use here is using decorators - you've possibly looked to tutorial about decorators instead strings (this is quite nice and easy example to learn decorators, but decorators itself are quite complex for starters and can be confusing...)

For basic idea, decorators are methods, that wraps around another methods... let's say, you want to log time before method and after it. You could do that in that methods body, but your method should only be focused on doing one thing. E.g. method add should only add not, measure time, add, then measure time. Also, this binds measuring to your method - therefore you cannot use your method without measurement. Doesn't sound too flexible, and starts to be bit messy too...

So decorator is a nice way to have your 'inner' method body clean and focused on its role, but extending it with another functionality... Wrapping method and inner method are also independent (most of cases), and can be quickly separated... Also imagine, that you want to measure time of 'a lot of' your methods, but not every - so you put that decorator only with those methods, you want.

user2678074
  • 768
  • 3
  • 9
  • 22
  • But if arguments are in tuple can i use format or f-string method? – Lukky Bakky Jul 05 '22 at 12:47
  • join takes as argument iterable, and tuple is an iterable. to makeit VERY EASY to you... tuple is a non-modify'able (but still iterable) list... If you can use for loop for list of strings, a for loop for tuple of strings is same thing ;P – user2678074 Jul 05 '22 at 12:55