python accessing variable of high level from lower level class

python access class variable from method
python class attributes
python class variables
python get class attributes
python update class attributes
python add attribute to class
python class attributes list
python class property

I have a class inside a class like this:

class Robot(object):
      def __init__(self):
         self.name = 'ganken'
       class Speaker(object):
          pass

Let’s say I want to access Robot.name from Speaker, how do I do that

You can make Speaker a sub class of Robot:

class Robot(object):
    def __init__(self):
        self.name = 'ganken'

class Speaker(Robot):
    def get_name(self):
        return self.name

When you make an instance of Speaker, it will now have the name ganken associated with it.

s = Speaker
s.get_name()

Output:

'ganken'

You could have also called s.name and it would have still returned 'ganken', but usually you want to define functions in your class to get and set different attributes of your class (look up getters and setters if you want more information).


I might have misunderstood your question. If you want to access the name of a instance of a Robot class in your Speaker class (and don't necessarily want to make Speaker a subclass of Robot), you could define your class as:

class Robot(object):
    def __init__(self):
        self.name = 'ganken'

class Speaker(object):
    def get_robot_name(self, robot):
        return robot.name

Notice that you can pass a robot instance into the get_robot_name function in this example.

Demo:

r = Robot()
s = Speaker()
s.get_robot_name(r)

'ganken'

Python Class Attributes: Examples of Variables, How do you access variables from other classes in Python? Python’s Thread class supports a subset of the behavior of Java’s Thread class; currently, there are no priorities, no thread groups, and threads cannot be destroyed, stopped, suspended, resumed, or interrupted. The static methods of Java’s Thread class, when implemented, are mapped to module-level functions.

You could try the following where you create a speaker object in the Robot class if you are looking for a nested class solution. But i don't know what is the reason for you to do this though.

Try this assuming Python3 environment:

class Robot(object):
    def __init__(self):
        self.name = 'ganken'
        self.speaker = self.createSpeaker()

    def createSpeaker(self):
        return Robot.Speaker(self)

    class Speaker:
        def __init__(self,robot):
            self.robot = robot
        def say_name(self):
            return f'Name: {self.robot.name}'

robot = Robot()
print(robot.speaker.say_name())

Variables and scope, How do you pass a variable from one class to another in python? how to access module level variable in class. Python Forums on Bytes.

Try the below:

print(Robot().name)

Just call it then access it

Output is:

ganken

Python, How do you access the outer class variable from the inner class in Python? All child-class stream reading should be done in the child class. This example is over simplified. The constant pool is not the only variable i need available to all subclasses. The idx variable is not the only thing read from the stream reader. Is this even possible in python? Is there no way to access the parent's information?

Python Tutorial: Class vs. Instance Attributes, This is no good—altering the class variable via one instance alters it for all the others! At the namespace level… all instances of Service are accessing and  A lower level wrapping around PyObject* is python::handle. self. When a python class is being inherited from a c++ class, one must write a c++ wrapper around the c++ class. This wrapper must have a pointer to a PyObject called self. It's the Python object which contains the C++ object instance. Memory consumption. In general, a wrapped C++

9. Classes, To define a new variable in Python, we simply assign a value to a label. If we try to access the value of a variable which hasn't been defined anywhere yet, the The inside of a class body is also a new local variable scope. The addition and subtraction are at the same level of precedence, so they are evaluated  Operating system interfaces, including functions to work with files at a lower level than Python file objects. Module io Python’s built-in I/O library, including both abstract classes and some concrete classes such as file I/O. Built-in function open() The standard way to open files for reading and writing with Python.

Pro Python System Administration, In Python, we can define the variable outside the class, inside the class, and even inside the methods. Let's see, how to use and access these variables  Python string method lower() returns a copy of the string in which all case-based characters have been lowercased. This method returns a copy of the string in which all case-based characters have been lowercased

Comments
  • Why are you nesting classes? It offers no advantage in Python.
  • There is no Robot.name. You only define name on an instance. For this question to mean anything, you have to show what you're trying to do.
  • And don't forget to indent your code properly.
  • Thanks a lot, I’m studying python through my senior code and there are a bunch classes inside class which make me confused a little bit, but I got my answer now.