sung chul
sung chul

Reputation: 39

how to access or use name in class ? Python

Python Masters. I am so curious about class name space. for example, there some class definition.

import Another_class

class Some_class:
    def __init__(self, city):
        self.city = city
        if self.city == "newyork":
            newyork_info = Another_class(some_param)

    def state(self):
        if self.city == "newyork":
           newyork_wether = newyork_info.get_wether()  
           newyork_population = newyork_info.get_population()  

so, i tend to use "newyork_info" in another functions.

NameError: name 'newyork_info' is not defined

but i could not use the name in init function. how could i solve it? Is there are good way? :)

Upvotes: 1

Views: 19

Answers (1)

Ricardo Lohmann
Ricardo Lohmann

Reputation: 26320

You must assign newyork_info to the class.

self.newyork_info = Another_class(some_param)

Upvotes: 2

Related Questions