How do you append a class object to a list in Python
Im attempting to append a class object to a list, the class being from a different file
heres the source code from main.py:
environmentVector =
environment.environment1 = environment.environment(100, 100, 32, 32)
environmentVector.append(environment.environment1)
and heres the class from environment.py:
class environment():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.x1 = x - 16
self.x2 = x + 16
self.y1 = y - 16
self.y2 = y + 16
this code throws an error saying
AttributeError: module 'environment' has no attribute 'environmentVector'
python list
|
show 6 more comments
Im attempting to append a class object to a list, the class being from a different file
heres the source code from main.py:
environmentVector =
environment.environment1 = environment.environment(100, 100, 32, 32)
environmentVector.append(environment.environment1)
and heres the class from environment.py:
class environment():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.x1 = x - 16
self.x2 = x + 16
self.y1 = y - 16
self.y2 = y + 16
this code throws an error saying
AttributeError: module 'environment' has no attribute 'environmentVector'
python list
1
You're never instantiating your class:environmentVector.append(environment(5, 2, 1, 3))
– TheIncorrigible1
Nov 26 '18 at 18:33
1
What do you mean by class object? Do you mean an instance of the class, or the class itself? Usually, "class object" means the class itself in python.
– juanpa.arrivillaga
Nov 26 '18 at 18:35
@TheIncorrigible1 that's going to raise something about module objects not being callable. The OP has neglected to say that they are importing a module with the same name as the class into in theirmain.pymodule, I suspect because they are coming from Java
– juanpa.arrivillaga
Nov 26 '18 at 18:36
The error has nothing to do with appending to a list. Please show the full traceback.
– roganjosh
Nov 26 '18 at 18:37
1
@AdamSmith then why are they gettingAttributeError: module 'environment' has no attribute 'x1'?
– juanpa.arrivillaga
Nov 26 '18 at 18:40
|
show 6 more comments
Im attempting to append a class object to a list, the class being from a different file
heres the source code from main.py:
environmentVector =
environment.environment1 = environment.environment(100, 100, 32, 32)
environmentVector.append(environment.environment1)
and heres the class from environment.py:
class environment():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.x1 = x - 16
self.x2 = x + 16
self.y1 = y - 16
self.y2 = y + 16
this code throws an error saying
AttributeError: module 'environment' has no attribute 'environmentVector'
python list
Im attempting to append a class object to a list, the class being from a different file
heres the source code from main.py:
environmentVector =
environment.environment1 = environment.environment(100, 100, 32, 32)
environmentVector.append(environment.environment1)
and heres the class from environment.py:
class environment():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.x1 = x - 16
self.x2 = x + 16
self.y1 = y - 16
self.y2 = y + 16
this code throws an error saying
AttributeError: module 'environment' has no attribute 'environmentVector'
python list
python list
edited Nov 26 '18 at 18:43
agtv
asked Nov 26 '18 at 18:32
agtvagtv
578
578
1
You're never instantiating your class:environmentVector.append(environment(5, 2, 1, 3))
– TheIncorrigible1
Nov 26 '18 at 18:33
1
What do you mean by class object? Do you mean an instance of the class, or the class itself? Usually, "class object" means the class itself in python.
– juanpa.arrivillaga
Nov 26 '18 at 18:35
@TheIncorrigible1 that's going to raise something about module objects not being callable. The OP has neglected to say that they are importing a module with the same name as the class into in theirmain.pymodule, I suspect because they are coming from Java
– juanpa.arrivillaga
Nov 26 '18 at 18:36
The error has nothing to do with appending to a list. Please show the full traceback.
– roganjosh
Nov 26 '18 at 18:37
1
@AdamSmith then why are they gettingAttributeError: module 'environment' has no attribute 'x1'?
– juanpa.arrivillaga
Nov 26 '18 at 18:40
|
show 6 more comments
1
You're never instantiating your class:environmentVector.append(environment(5, 2, 1, 3))
– TheIncorrigible1
Nov 26 '18 at 18:33
1
What do you mean by class object? Do you mean an instance of the class, or the class itself? Usually, "class object" means the class itself in python.
– juanpa.arrivillaga
Nov 26 '18 at 18:35
@TheIncorrigible1 that's going to raise something about module objects not being callable. The OP has neglected to say that they are importing a module with the same name as the class into in theirmain.pymodule, I suspect because they are coming from Java
– juanpa.arrivillaga
Nov 26 '18 at 18:36
The error has nothing to do with appending to a list. Please show the full traceback.
– roganjosh
Nov 26 '18 at 18:37
1
@AdamSmith then why are they gettingAttributeError: module 'environment' has no attribute 'x1'?
– juanpa.arrivillaga
Nov 26 '18 at 18:40
1
1
You're never instantiating your class:
environmentVector.append(environment(5, 2, 1, 3))– TheIncorrigible1
Nov 26 '18 at 18:33
You're never instantiating your class:
environmentVector.append(environment(5, 2, 1, 3))– TheIncorrigible1
Nov 26 '18 at 18:33
1
1
What do you mean by class object? Do you mean an instance of the class, or the class itself? Usually, "class object" means the class itself in python.
– juanpa.arrivillaga
Nov 26 '18 at 18:35
What do you mean by class object? Do you mean an instance of the class, or the class itself? Usually, "class object" means the class itself in python.
– juanpa.arrivillaga
Nov 26 '18 at 18:35
@TheIncorrigible1 that's going to raise something about module objects not being callable. The OP has neglected to say that they are importing a module with the same name as the class into in their
main.py module, I suspect because they are coming from Java– juanpa.arrivillaga
Nov 26 '18 at 18:36
@TheIncorrigible1 that's going to raise something about module objects not being callable. The OP has neglected to say that they are importing a module with the same name as the class into in their
main.py module, I suspect because they are coming from Java– juanpa.arrivillaga
Nov 26 '18 at 18:36
The error has nothing to do with appending to a list. Please show the full traceback.
– roganjosh
Nov 26 '18 at 18:37
The error has nothing to do with appending to a list. Please show the full traceback.
– roganjosh
Nov 26 '18 at 18:37
1
1
@AdamSmith then why are they getting
AttributeError: module 'environment' has no attribute 'x1'?– juanpa.arrivillaga
Nov 26 '18 at 18:40
@AdamSmith then why are they getting
AttributeError: module 'environment' has no attribute 'x1'?– juanpa.arrivillaga
Nov 26 '18 at 18:40
|
show 6 more comments
1 Answer
1
active
oldest
votes
You've got two problems. First of all when you do:
import environment
that sets environment to the namespace that contains your environment class, not to the class itself. This is different from some other OOP languages (Java, for instance).
# My sample environment.py
class Environment(object):
pass
foo = "bar"
# my sample main.py
import environment
# environment.Environment is the class
# environment.Environment() is an instance of that class.
# environment.foo is "bar"
The first problem is that you're using environment where you should use environment.environment. The second problem is that you're using environment when you should actually be using environment.environment(some_x, some_y, some_width, some_height). You need to instantiate your class before trying to use it as an instance!
the original problem is fixed thanks everyone
– agtv
Nov 26 '18 at 18:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487069%2fhow-do-you-append-a-class-object-to-a-list-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You've got two problems. First of all when you do:
import environment
that sets environment to the namespace that contains your environment class, not to the class itself. This is different from some other OOP languages (Java, for instance).
# My sample environment.py
class Environment(object):
pass
foo = "bar"
# my sample main.py
import environment
# environment.Environment is the class
# environment.Environment() is an instance of that class.
# environment.foo is "bar"
The first problem is that you're using environment where you should use environment.environment. The second problem is that you're using environment when you should actually be using environment.environment(some_x, some_y, some_width, some_height). You need to instantiate your class before trying to use it as an instance!
the original problem is fixed thanks everyone
– agtv
Nov 26 '18 at 18:49
add a comment |
You've got two problems. First of all when you do:
import environment
that sets environment to the namespace that contains your environment class, not to the class itself. This is different from some other OOP languages (Java, for instance).
# My sample environment.py
class Environment(object):
pass
foo = "bar"
# my sample main.py
import environment
# environment.Environment is the class
# environment.Environment() is an instance of that class.
# environment.foo is "bar"
The first problem is that you're using environment where you should use environment.environment. The second problem is that you're using environment when you should actually be using environment.environment(some_x, some_y, some_width, some_height). You need to instantiate your class before trying to use it as an instance!
the original problem is fixed thanks everyone
– agtv
Nov 26 '18 at 18:49
add a comment |
You've got two problems. First of all when you do:
import environment
that sets environment to the namespace that contains your environment class, not to the class itself. This is different from some other OOP languages (Java, for instance).
# My sample environment.py
class Environment(object):
pass
foo = "bar"
# my sample main.py
import environment
# environment.Environment is the class
# environment.Environment() is an instance of that class.
# environment.foo is "bar"
The first problem is that you're using environment where you should use environment.environment. The second problem is that you're using environment when you should actually be using environment.environment(some_x, some_y, some_width, some_height). You need to instantiate your class before trying to use it as an instance!
You've got two problems. First of all when you do:
import environment
that sets environment to the namespace that contains your environment class, not to the class itself. This is different from some other OOP languages (Java, for instance).
# My sample environment.py
class Environment(object):
pass
foo = "bar"
# my sample main.py
import environment
# environment.Environment is the class
# environment.Environment() is an instance of that class.
# environment.foo is "bar"
The first problem is that you're using environment where you should use environment.environment. The second problem is that you're using environment when you should actually be using environment.environment(some_x, some_y, some_width, some_height). You need to instantiate your class before trying to use it as an instance!
answered Nov 26 '18 at 18:44
Adam SmithAdam Smith
34.1k53275
34.1k53275
the original problem is fixed thanks everyone
– agtv
Nov 26 '18 at 18:49
add a comment |
the original problem is fixed thanks everyone
– agtv
Nov 26 '18 at 18:49
the original problem is fixed thanks everyone
– agtv
Nov 26 '18 at 18:49
the original problem is fixed thanks everyone
– agtv
Nov 26 '18 at 18:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487069%2fhow-do-you-append-a-class-object-to-a-list-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You're never instantiating your class:
environmentVector.append(environment(5, 2, 1, 3))– TheIncorrigible1
Nov 26 '18 at 18:33
1
What do you mean by class object? Do you mean an instance of the class, or the class itself? Usually, "class object" means the class itself in python.
– juanpa.arrivillaga
Nov 26 '18 at 18:35
@TheIncorrigible1 that's going to raise something about module objects not being callable. The OP has neglected to say that they are importing a module with the same name as the class into in their
main.pymodule, I suspect because they are coming from Java– juanpa.arrivillaga
Nov 26 '18 at 18:36
The error has nothing to do with appending to a list. Please show the full traceback.
– roganjosh
Nov 26 '18 at 18:37
1
@AdamSmith then why are they getting
AttributeError: module 'environment' has no attribute 'x1'?– juanpa.arrivillaga
Nov 26 '18 at 18:40