-4

Please look at the below code:

def __init__(self):
    self.job = Job('today', 10)

def createList(self):
    return [self.job(date=self.date,
                             selary=cv.salary)
            for cv in self.cvItems]

I don't understand the syntax of the createList method. Can you please write it in a more simple way for Java developers to understand.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • 1
    why should Python developer write a code understandable to Java developer? It is Python, your code should be syntactically related to Python *(and it should be Pythonic)* – Moinuddin Quadri Feb 20 '18 at 16:39
  • Haven't you asked and deleted several questions about list comprehensions? – castis Feb 20 '18 at 16:39
  • @MoinuddinQuadri so java developer wanting to become a python developer will understand the syntax of this method. – Ilya Gazman Feb 20 '18 at 16:40
  • @castis yeah I asked a similar question earlier, but I described it poorly and had to delete it. I hope that this time I got it right. I still don't get this syntax and not able to find a good example that will explain it to me. So I am hoping to get an answer in here. – Ilya Gazman Feb 20 '18 at 16:42

1 Answers1

1

Translate it into a for loop if you don't get it at first.

result = []
for cv in self.cvItems:
   result.append(self.job(date=self.date, selary=cv.selary))
return result

unrelated: it's spelled salary.

Compared:

[self.job(date=self.date, selary=cv.selary) for cv in self.cvItems]
#     (1)                                       (2)        (3)

for cv in self.cvItems:
#   (2)        (3)
    result.append(self.job(date=self.date, selary=cv.selary))
#                      (1)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • So in Python, you are allowed to call a constructor of an object from its instance? I was expecting to see there Job(...) instead of self.job(...) – Ilya Gazman Feb 20 '18 at 16:45
  • @Ilya_Gazman nope, you can't. `Job` must be a callable. Since I don't have any implementation details of `Job` I can't say any more than that. Regardless it's not really relevant to the list comprehension syntax you're asking about ;) – Adam Smith Feb 20 '18 at 16:45
  • This is why I provided the init method, so you can see how a job is been initialized, it's clear that a job is an object from Job instance. – Ilya Gazman Feb 20 '18 at 16:48
  • @Ilya_Gazman sure, but `Job(...)` might still be callable. Again: you haven't provided anything to let me know what a `Job` is, just that `self.job` *is* one. And again: the specifics of `Job` is off-topic to the question you asked, which is about the list comp syntax. – Adam Smith Feb 20 '18 at 16:49
  • Oh wow, so you are saying that even with the init method that I have their(let's consider it part of the context of this question) and assuming that no one overrides self.job, the syntax in the createList can still be valid? If the answer is yes(like in my project that I am trying to figure out) can you please explain to me how can it be? – Ilya Gazman Feb 20 '18 at 16:53
  • @Ilya_Gazman the `Job` instance must itself be [callable](https://stackoverflow.com/questions/111234/what-is-a-callable-in-python) – Adam Smith Feb 20 '18 at 16:54
  • Thank you so much, this was the part that I wasn't understanding the **callable** thing. Thank you! – Ilya Gazman Feb 20 '18 at 16:57