Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Magic method __str__
#11
@nilamo no, I don´t want to copy a list inside a function, all what I need is this function should work for changes in a list. For example, after __init__ (which creates a list self.tab) I want __str__() to create a string from this list. Using the code by @buran few posts above it works, but I need to call this method also later, after some changes in this self.tab happen. And these are specifically "." changed to a set. After this, method __str__() create a string consisting of numbers and sets, but I still want to put "." instead of sets here.
Reply
#12
UP....
Reply
#13
def __str__(self):
    lines = []
    for sub_list in self.tab:
        lines.append(' '.join([str(item) for item in sub_list if not isinstance(item, set) else '.']))
    return '\n'.join(lines)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
This raises me an invalid syntax error and not sure what is wrong there.. :/
Reply
#15
Sorry, that should be:

def __str__(self):
    lines = []
    for sub_list in self.tab:
        lines.append(' '.join([str(item) if not isinstance(item, set) else '.' for item in sub_list]))
    return '\n'.join(lines)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#16
@ichabod801 thanks, this seems working, the only thing missing (I forgot to mentioned that, but I guess it´s just an addition into this generator notation) - if length of set is 1, it should put that one item (number) into that string, instead of "."
Is it possible to add "elif" statement there?
Reply
#17
No, you can't put an elif in a ternary statement. You would have to break that list comprehension out into a loop with a full conditional statement.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  magic related field in Django model sonh 1 1,201 Apr-24-2022, 12:37 PM
Last Post: sonh
  Need a little help with numpy array magic. pmf71 0 1,126 Dec-01-2021, 02:51 AM
Last Post: pmf71
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,325 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  Magic Method Arithmetic Operators ClownPrinceOfCrime 3 2,280 Jan-10-2021, 03:24 PM
Last Post: ndc85430
  How to eliminate magic squares formed by the same numbers, but permuted frame 7 3,554 May-09-2019, 11:28 AM
Last Post: frame
  TypeError: __str__ returned non-string error jolinchewjb 5 9,966 Jan-24-2019, 07:54 AM
Last Post: jolinchewjb
  [split] Bad magic number. What is it ? Douglas 2 5,205 Oct-22-2018, 10:38 AM
Last Post: Douglas
  How to add metakernel magic for python rikaki__ 0 1,991 Jul-04-2018, 02:49 AM
Last Post: rikaki__
  Bad magic number. What is it ? sylas 4 7,747 Sep-20-2017, 05:52 PM
Last Post: sylas
  How to Create Very Very Special Class with too many magic methods ? harun2525 5 4,305 Apr-13-2017, 10:18 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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