I was playing with classes and I thought to create a class called Container which is meant to group all things that can hold other things.
# -*- coding: utf-8 -*-
class Container(object):
def __init__(self, name, volume):
self.name = name
self.max_volume = volume
hands = Container('HANDS', 2)
I started from this point and then I wanted to test if everything was ok but if in the python console I call hands.name
it says that hands
is not defined. This happens when I import it as a module too.
I don't get what I am doint wrong! Can you please explain me how to make it work?
I get from the python console:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'hands' is not defined
Assuming this is the container.py
file:
# -*- coding: utf-8 -*-
class Container(object):
def __init__(self, name, volume):
self.name = name
self.max_volume = volume
hands = Container('HANDS', 2)
and this is your execution command in the interpreter:
>>> import container
Then, the hands
instance is accessible in the following manner:
>>> container.hands.name
To avoid the container
prefix, you can also do this:
>>> from container import hands
>>> hands.name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.