I can't do the questions while studying Python class. The wrong place

Asked 2 years ago, Updated 2 years ago, 118 views

I'm studying about Python class! I defined the class and proceeded, but it was executed, but the result didn't come out I ask for your help me.

class myObj(object):
    def set_value(self, item):
        self.item=item

    def get_value(self):
        return self.item

    def M_calc(self):
        self.item=self.item+10
        return self.item

if __name__ == "__name__":
    result=[]
    obj=myObj()
    obj.set_value(100)
    result.append(obj)

    obj01=myObj()
    obj01.set_value(200)
    result.append(obj01)

    map(lambda item:item.M_calc(), result)
    for m_obj in result:
        print(m_obj.get_value())

The desired execution result is

100

200

It's printed out.

python class

2022-09-22 20:16

1 Answers

Please change the if statement as below.

if __name__ == "__main__":

if_name__=="_main_": is required to know if the script runs directly as an interpreter or by importing it from another module. If it runs directly, it acts as a starting point (the main function of Java).

Check here for the if__name_=="_main_": Meaningchapter.

There's a good explanation in the hash code. http://hashcode.co.kr/questions/3/if-__name__-__main__ is a-why do you write


2022-09-22 20:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.