How can i add my objects into a list to choose them randomly to blit onto the screen?How to define a __str__ method for a class?How to randomly select an item from a list?How to sort a list of objects based on an attribute of the objects?How can I get a list of locally installed Python modules?How to check if an object is a list or tuple (but not string)?How can I count the occurrences of a list item?How can I create an object and add attributes to it?How can I reverse a list in Python?How can I get the concatenation of two lists in Python without modifying either one?How do I find the duplicates in a list and create another list with them?Convert list of strings into objects
Operation Unz̖̬̜̺̬a͇͖̯͔͉l̟̭g͕̝̼͇͓̪͍o̬̝͍̹̻
Is this Android phone Android 9.0 or Android 6.0?
What "fuel more powerful than anything the West (had) in stock" put Laika in orbit aboard Sputnik 2?
Should I be able to keep my company purchased standing desk when I leave my job?
Strategy to pay off revolving debt while building reserve savings fund?
Is it ethical for a company to ask its employees to move furniture on a weekend?
Was Apollo 13 radio blackout on reentry longer than expected?
Improve quality of image bars
How would you say "Sorry, that was a mistake on my part"?
Why doesn't philosophy have higher standards for its arguments?
Cauchy reals and Dedekind reals satisfy "the same mathematical theorems"
Is passive Investigation essentially truesight against illusions?
What does it actually mean to have two time dimensions?
What are the first usages of "thong" as a wearable item of clothing, both on the feet and on the waist?
How fast does a character need to move to be effectively invisible?
Did 007 exist before James Bond?
What is the meaning of [[:space:]] in bash?
How should one refer to knights (& dames) in academic writing?
ROT13 encoder/decoder
What happens if there is no space for entry stamp in the passport for US visa?
Which GPUs to get for Mathematical Optimization (if any...)?
How to remove the first colon ':' from a timestamp?
How can electric field be defined as force per charge, if the charge makes its own, singular electric field?
What prompted Cuba to fight against South African Imperialism?
How can i add my objects into a list to choose them randomly to blit onto the screen?
How to define a __str__ method for a class?How to randomly select an item from a list?How to sort a list of objects based on an attribute of the objects?How can I get a list of locally installed Python modules?How to check if an object is a list or tuple (but not string)?How can I count the occurrences of a list item?How can I create an object and add attributes to it?How can I reverse a list in Python?How can I get the concatenation of two lists in Python without modifying either one?How do I find the duplicates in a list and create another list with them?Convert list of strings into objects
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
When i try to put my objects into a list, i can not get an output with object names, it gives a weird output like "_ main _.object at 0x029E7210". I want to select my objects randomly to blit ONE of them onto the screen. But i could not figure this out.
car_main = pygame.image.load("car_main.png")
car_red_ = pygame.image.load("car_red.png")
car_blue = pygame.image.load("car_blue.png")
class cars:
def __init__(self,x,y,car_type,w=50,h=100,s=5):
self.x = x
self.y = y
self.w = w
self.h = h
self.s = s
self.car_type = car_type
def draw(self):
dp.blit(self.car_type,(self.x,self.y))
car1 = cars(x,y,car_main)
car2 = cars(x,y,car_red)
car3 = cars(x,y,car_blue)
car_list = [car1,car2,car3]
rc = random.choice(car_list)
print(rc)
# output> __main__.object at 0x02A97230
When I change
car_list = [car1,car2,car3] with;
car_list = [car1.car_type,car2.car_type,car3.car_type]
# output > Surface(50x100x32 SW)
But I want to see an output as my object names. Not as a string type ("car_main"). I want to get an output as the object name (car_main) directly. Because in the main loop, i will choose one of them to blit onto the screen everytime when the loop renews itself.
python
add a comment |
When i try to put my objects into a list, i can not get an output with object names, it gives a weird output like "_ main _.object at 0x029E7210". I want to select my objects randomly to blit ONE of them onto the screen. But i could not figure this out.
car_main = pygame.image.load("car_main.png")
car_red_ = pygame.image.load("car_red.png")
car_blue = pygame.image.load("car_blue.png")
class cars:
def __init__(self,x,y,car_type,w=50,h=100,s=5):
self.x = x
self.y = y
self.w = w
self.h = h
self.s = s
self.car_type = car_type
def draw(self):
dp.blit(self.car_type,(self.x,self.y))
car1 = cars(x,y,car_main)
car2 = cars(x,y,car_red)
car3 = cars(x,y,car_blue)
car_list = [car1,car2,car3]
rc = random.choice(car_list)
print(rc)
# output> __main__.object at 0x02A97230
When I change
car_list = [car1,car2,car3] with;
car_list = [car1.car_type,car2.car_type,car3.car_type]
# output > Surface(50x100x32 SW)
But I want to see an output as my object names. Not as a string type ("car_main"). I want to get an output as the object name (car_main) directly. Because in the main loop, i will choose one of them to blit onto the screen everytime when the loop renews itself.
python
It's printing an object's address in memory if you didn't define__str__method for it.
– knh190
Mar 26 at 9:03
what kind of a code do i need?
– Arda Altun
Mar 26 at 9:05
You need a__str__for desired class stackoverflow.com/questions/8144026/…
– knh190
Mar 26 at 9:07
Also you need to find the variable name inglobals()orlocals()- depends on your scope.
– knh190
Mar 26 at 9:10
add a comment |
When i try to put my objects into a list, i can not get an output with object names, it gives a weird output like "_ main _.object at 0x029E7210". I want to select my objects randomly to blit ONE of them onto the screen. But i could not figure this out.
car_main = pygame.image.load("car_main.png")
car_red_ = pygame.image.load("car_red.png")
car_blue = pygame.image.load("car_blue.png")
class cars:
def __init__(self,x,y,car_type,w=50,h=100,s=5):
self.x = x
self.y = y
self.w = w
self.h = h
self.s = s
self.car_type = car_type
def draw(self):
dp.blit(self.car_type,(self.x,self.y))
car1 = cars(x,y,car_main)
car2 = cars(x,y,car_red)
car3 = cars(x,y,car_blue)
car_list = [car1,car2,car3]
rc = random.choice(car_list)
print(rc)
# output> __main__.object at 0x02A97230
When I change
car_list = [car1,car2,car3] with;
car_list = [car1.car_type,car2.car_type,car3.car_type]
# output > Surface(50x100x32 SW)
But I want to see an output as my object names. Not as a string type ("car_main"). I want to get an output as the object name (car_main) directly. Because in the main loop, i will choose one of them to blit onto the screen everytime when the loop renews itself.
python
When i try to put my objects into a list, i can not get an output with object names, it gives a weird output like "_ main _.object at 0x029E7210". I want to select my objects randomly to blit ONE of them onto the screen. But i could not figure this out.
car_main = pygame.image.load("car_main.png")
car_red_ = pygame.image.load("car_red.png")
car_blue = pygame.image.load("car_blue.png")
class cars:
def __init__(self,x,y,car_type,w=50,h=100,s=5):
self.x = x
self.y = y
self.w = w
self.h = h
self.s = s
self.car_type = car_type
def draw(self):
dp.blit(self.car_type,(self.x,self.y))
car1 = cars(x,y,car_main)
car2 = cars(x,y,car_red)
car3 = cars(x,y,car_blue)
car_list = [car1,car2,car3]
rc = random.choice(car_list)
print(rc)
# output> __main__.object at 0x02A97230
When I change
car_list = [car1,car2,car3] with;
car_list = [car1.car_type,car2.car_type,car3.car_type]
# output > Surface(50x100x32 SW)
But I want to see an output as my object names. Not as a string type ("car_main"). I want to get an output as the object name (car_main) directly. Because in the main loop, i will choose one of them to blit onto the screen everytime when the loop renews itself.
python
python
edited Mar 26 at 9:49
knh190
1,7831 gold badge8 silver badges24 bronze badges
1,7831 gold badge8 silver badges24 bronze badges
asked Mar 26 at 9:01
Arda AltunArda Altun
381 silver badge7 bronze badges
381 silver badge7 bronze badges
It's printing an object's address in memory if you didn't define__str__method for it.
– knh190
Mar 26 at 9:03
what kind of a code do i need?
– Arda Altun
Mar 26 at 9:05
You need a__str__for desired class stackoverflow.com/questions/8144026/…
– knh190
Mar 26 at 9:07
Also you need to find the variable name inglobals()orlocals()- depends on your scope.
– knh190
Mar 26 at 9:10
add a comment |
It's printing an object's address in memory if you didn't define__str__method for it.
– knh190
Mar 26 at 9:03
what kind of a code do i need?
– Arda Altun
Mar 26 at 9:05
You need a__str__for desired class stackoverflow.com/questions/8144026/…
– knh190
Mar 26 at 9:07
Also you need to find the variable name inglobals()orlocals()- depends on your scope.
– knh190
Mar 26 at 9:10
It's printing an object's address in memory if you didn't define
__str__ method for it.– knh190
Mar 26 at 9:03
It's printing an object's address in memory if you didn't define
__str__ method for it.– knh190
Mar 26 at 9:03
what kind of a code do i need?
– Arda Altun
Mar 26 at 9:05
what kind of a code do i need?
– Arda Altun
Mar 26 at 9:05
You need a
__str__ for desired class stackoverflow.com/questions/8144026/…– knh190
Mar 26 at 9:07
You need a
__str__ for desired class stackoverflow.com/questions/8144026/…– knh190
Mar 26 at 9:07
Also you need to find the variable name in
globals() or locals() - depends on your scope.– knh190
Mar 26 at 9:10
Also you need to find the variable name in
globals() or locals() - depends on your scope.– knh190
Mar 26 at 9:10
add a comment |
2 Answers
2
active
oldest
votes
You need to define __str__ for your class Car to let it properly handle object to string:
class Car:
def __str__(self):
for k, var in globals().items():
if var == self:
return k
# default
return "Car"
Note1: Usually use uppercased Car for a class and car for an instance.
Note2: Look up variable strings in globals is not reliable. You may not want to make all variables global, and manually search them in scope is tedious. Actually why don't you give your Car a name attribute? Then you nicely have:
class Car:
def __init__(self, name):
self.name=name
def __str__(self):
return self.name
car = Car(name='first car')
print(car) # 'first car'
More read about "magic methods": https://rszalski.github.io/magicmethods/#representations
i just added "name" into the init method. and i called my object (car1 = cars("car_red",x,y,car_type) and it gives the name. should i add str ? because it seems like it works currently
– Arda Altun
Mar 26 at 10:42
@ArdaAltun Well, not 100% sure with your current code, but you should solve the perplex after reading the referenced link. It explains why/when you should define__str__()
– knh190
Mar 26 at 14:24
add a comment |
Add a __str()__ magic method to your car class like so:
def __str__(self):
return f'car with x of self.x, y of self.y, and type of self.car_type'
It should not be aprint!
– knh190
Mar 26 at 9:14
Silly mistake! My bad. Edited toreturn
– Alec Alameddine
Mar 26 at 9:15
And OP actually wanted to print the variable string instead of inspecting :) while that's not a good idea.
– knh190
Mar 26 at 9:24
how can i call this function? what should i type? when i type print(car.__str__) it gives a weird output
– Arda Altun
Mar 26 at 10:39
you can do that but once you've defined a str function you can just print car. If you don't like the output change the return
– Alec Alameddine
Mar 26 at 10:40
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%2f55353220%2fhow-can-i-add-my-objects-into-a-list-to-choose-them-randomly-to-blit-onto-the-sc%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to define __str__ for your class Car to let it properly handle object to string:
class Car:
def __str__(self):
for k, var in globals().items():
if var == self:
return k
# default
return "Car"
Note1: Usually use uppercased Car for a class and car for an instance.
Note2: Look up variable strings in globals is not reliable. You may not want to make all variables global, and manually search them in scope is tedious. Actually why don't you give your Car a name attribute? Then you nicely have:
class Car:
def __init__(self, name):
self.name=name
def __str__(self):
return self.name
car = Car(name='first car')
print(car) # 'first car'
More read about "magic methods": https://rszalski.github.io/magicmethods/#representations
i just added "name" into the init method. and i called my object (car1 = cars("car_red",x,y,car_type) and it gives the name. should i add str ? because it seems like it works currently
– Arda Altun
Mar 26 at 10:42
@ArdaAltun Well, not 100% sure with your current code, but you should solve the perplex after reading the referenced link. It explains why/when you should define__str__()
– knh190
Mar 26 at 14:24
add a comment |
You need to define __str__ for your class Car to let it properly handle object to string:
class Car:
def __str__(self):
for k, var in globals().items():
if var == self:
return k
# default
return "Car"
Note1: Usually use uppercased Car for a class and car for an instance.
Note2: Look up variable strings in globals is not reliable. You may not want to make all variables global, and manually search them in scope is tedious. Actually why don't you give your Car a name attribute? Then you nicely have:
class Car:
def __init__(self, name):
self.name=name
def __str__(self):
return self.name
car = Car(name='first car')
print(car) # 'first car'
More read about "magic methods": https://rszalski.github.io/magicmethods/#representations
i just added "name" into the init method. and i called my object (car1 = cars("car_red",x,y,car_type) and it gives the name. should i add str ? because it seems like it works currently
– Arda Altun
Mar 26 at 10:42
@ArdaAltun Well, not 100% sure with your current code, but you should solve the perplex after reading the referenced link. It explains why/when you should define__str__()
– knh190
Mar 26 at 14:24
add a comment |
You need to define __str__ for your class Car to let it properly handle object to string:
class Car:
def __str__(self):
for k, var in globals().items():
if var == self:
return k
# default
return "Car"
Note1: Usually use uppercased Car for a class and car for an instance.
Note2: Look up variable strings in globals is not reliable. You may not want to make all variables global, and manually search them in scope is tedious. Actually why don't you give your Car a name attribute? Then you nicely have:
class Car:
def __init__(self, name):
self.name=name
def __str__(self):
return self.name
car = Car(name='first car')
print(car) # 'first car'
More read about "magic methods": https://rszalski.github.io/magicmethods/#representations
You need to define __str__ for your class Car to let it properly handle object to string:
class Car:
def __str__(self):
for k, var in globals().items():
if var == self:
return k
# default
return "Car"
Note1: Usually use uppercased Car for a class and car for an instance.
Note2: Look up variable strings in globals is not reliable. You may not want to make all variables global, and manually search them in scope is tedious. Actually why don't you give your Car a name attribute? Then you nicely have:
class Car:
def __init__(self, name):
self.name=name
def __str__(self):
return self.name
car = Car(name='first car')
print(car) # 'first car'
More read about "magic methods": https://rszalski.github.io/magicmethods/#representations
edited Mar 26 at 14:25
answered Mar 26 at 9:16
knh190knh190
1,7831 gold badge8 silver badges24 bronze badges
1,7831 gold badge8 silver badges24 bronze badges
i just added "name" into the init method. and i called my object (car1 = cars("car_red",x,y,car_type) and it gives the name. should i add str ? because it seems like it works currently
– Arda Altun
Mar 26 at 10:42
@ArdaAltun Well, not 100% sure with your current code, but you should solve the perplex after reading the referenced link. It explains why/when you should define__str__()
– knh190
Mar 26 at 14:24
add a comment |
i just added "name" into the init method. and i called my object (car1 = cars("car_red",x,y,car_type) and it gives the name. should i add str ? because it seems like it works currently
– Arda Altun
Mar 26 at 10:42
@ArdaAltun Well, not 100% sure with your current code, but you should solve the perplex after reading the referenced link. It explains why/when you should define__str__()
– knh190
Mar 26 at 14:24
i just added "name" into the init method. and i called my object (car1 = cars("car_red",x,y,car_type) and it gives the name. should i add str ? because it seems like it works currently
– Arda Altun
Mar 26 at 10:42
i just added "name" into the init method. and i called my object (car1 = cars("car_red",x,y,car_type) and it gives the name. should i add str ? because it seems like it works currently
– Arda Altun
Mar 26 at 10:42
@ArdaAltun Well, not 100% sure with your current code, but you should solve the perplex after reading the referenced link. It explains why/when you should define
__str__()– knh190
Mar 26 at 14:24
@ArdaAltun Well, not 100% sure with your current code, but you should solve the perplex after reading the referenced link. It explains why/when you should define
__str__()– knh190
Mar 26 at 14:24
add a comment |
Add a __str()__ magic method to your car class like so:
def __str__(self):
return f'car with x of self.x, y of self.y, and type of self.car_type'
It should not be aprint!
– knh190
Mar 26 at 9:14
Silly mistake! My bad. Edited toreturn
– Alec Alameddine
Mar 26 at 9:15
And OP actually wanted to print the variable string instead of inspecting :) while that's not a good idea.
– knh190
Mar 26 at 9:24
how can i call this function? what should i type? when i type print(car.__str__) it gives a weird output
– Arda Altun
Mar 26 at 10:39
you can do that but once you've defined a str function you can just print car. If you don't like the output change the return
– Alec Alameddine
Mar 26 at 10:40
add a comment |
Add a __str()__ magic method to your car class like so:
def __str__(self):
return f'car with x of self.x, y of self.y, and type of self.car_type'
It should not be aprint!
– knh190
Mar 26 at 9:14
Silly mistake! My bad. Edited toreturn
– Alec Alameddine
Mar 26 at 9:15
And OP actually wanted to print the variable string instead of inspecting :) while that's not a good idea.
– knh190
Mar 26 at 9:24
how can i call this function? what should i type? when i type print(car.__str__) it gives a weird output
– Arda Altun
Mar 26 at 10:39
you can do that but once you've defined a str function you can just print car. If you don't like the output change the return
– Alec Alameddine
Mar 26 at 10:40
add a comment |
Add a __str()__ magic method to your car class like so:
def __str__(self):
return f'car with x of self.x, y of self.y, and type of self.car_type'
Add a __str()__ magic method to your car class like so:
def __str__(self):
return f'car with x of self.x, y of self.y, and type of self.car_type'
edited Mar 26 at 9:15
answered Mar 26 at 9:12
Alec AlameddineAlec Alameddine
3,9034 gold badges13 silver badges40 bronze badges
3,9034 gold badges13 silver badges40 bronze badges
It should not be aprint!
– knh190
Mar 26 at 9:14
Silly mistake! My bad. Edited toreturn
– Alec Alameddine
Mar 26 at 9:15
And OP actually wanted to print the variable string instead of inspecting :) while that's not a good idea.
– knh190
Mar 26 at 9:24
how can i call this function? what should i type? when i type print(car.__str__) it gives a weird output
– Arda Altun
Mar 26 at 10:39
you can do that but once you've defined a str function you can just print car. If you don't like the output change the return
– Alec Alameddine
Mar 26 at 10:40
add a comment |
It should not be aprint!
– knh190
Mar 26 at 9:14
Silly mistake! My bad. Edited toreturn
– Alec Alameddine
Mar 26 at 9:15
And OP actually wanted to print the variable string instead of inspecting :) while that's not a good idea.
– knh190
Mar 26 at 9:24
how can i call this function? what should i type? when i type print(car.__str__) it gives a weird output
– Arda Altun
Mar 26 at 10:39
you can do that but once you've defined a str function you can just print car. If you don't like the output change the return
– Alec Alameddine
Mar 26 at 10:40
It should not be a
print!– knh190
Mar 26 at 9:14
It should not be a
print!– knh190
Mar 26 at 9:14
Silly mistake! My bad. Edited to
return– Alec Alameddine
Mar 26 at 9:15
Silly mistake! My bad. Edited to
return– Alec Alameddine
Mar 26 at 9:15
And OP actually wanted to print the variable string instead of inspecting :) while that's not a good idea.
– knh190
Mar 26 at 9:24
And OP actually wanted to print the variable string instead of inspecting :) while that's not a good idea.
– knh190
Mar 26 at 9:24
how can i call this function? what should i type? when i type print(car.__str__) it gives a weird output
– Arda Altun
Mar 26 at 10:39
how can i call this function? what should i type? when i type print(car.__str__) it gives a weird output
– Arda Altun
Mar 26 at 10:39
you can do that but once you've defined a str function you can just print car. If you don't like the output change the return
– Alec Alameddine
Mar 26 at 10:40
you can do that but once you've defined a str function you can just print car. If you don't like the output change the return
– Alec Alameddine
Mar 26 at 10:40
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%2f55353220%2fhow-can-i-add-my-objects-into-a-list-to-choose-them-randomly-to-blit-onto-the-sc%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
It's printing an object's address in memory if you didn't define
__str__method for it.– knh190
Mar 26 at 9:03
what kind of a code do i need?
– Arda Altun
Mar 26 at 9:05
You need a
__str__for desired class stackoverflow.com/questions/8144026/…– knh190
Mar 26 at 9:07
Also you need to find the variable name in
globals()orlocals()- depends on your scope.– knh190
Mar 26 at 9:10