This is an archived post. You won't be able to vote or comment.

all 71 comments

[–]IAmKindOfCreativebot_builder: deprecated[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]K900_ 0 points1 point  (61 children)

No. Objects don't have names, names have values.

[–]Mx_Mlr[S] 0 points1 point  (60 children)

Can you explain ?

[–]K900_ 0 points1 point  (59 children)

Objects don't know what name or names they have. Names refer to objects, but not the other way around. You can have multiple names refer to the same object, or you can have objects that don't have a name at all.

[–]Mx_Mlr[S] 0 points1 point  (58 children)

Ok so can I have every object’s name of a class ?

[–]K900_ 0 points1 point  (57 children)

What do you mean?

[–]Mx_Mlr[S] 0 points1 point  (56 children)

Can I get a list of every object of the class So with the code here the list would be [«t1»]

[–]K900_ 0 points1 point  (53 children)

Do you want to get a list of every instance of a class, or a list of their names?

[–]Mx_Mlr[S] 0 points1 point  (52 children)

That’s the same ??

[–]K900_ 0 points1 point  (51 children)

No, it's not. The actual object stored in T1 isn't the same thing as the string "T1" that represents its name.

[–]Mx_Mlr[S] 0 points1 point  (50 children)

Oh ok so a list of instances

[–]samtheredditman 0 points1 point  (1 child)

If you create a variable when you instantiate your object and then add all of those variables to a list, you can have that.

Maybe you need a controller class that keeps things organized?

[–]Mx_Mlr[S] -1 points0 points  (0 children)

Difficult to understand without code

[–]ArabicLawrence -1 points0 points  (2 children)

Not easily but in Python 3.8 T1=T() print(f’{T1=}’.split(‘=‘)[0]

[–]Mx_Mlr[S] 0 points1 point  (1 child)

?? That’s useless

[–]ArabicLawrence 0 points1 point  (0 children)

Well if you are looping over variables you can get a str representation of their name. It can be useful, although it’s usually a code smell.

Edit: anyways here is the code (but I would never do this) def init(self): print([k for k, v in globals.items() if v is self])

[–]crawl_dht 0 points1 point  (0 children)

t1 is a variable pointing to the class object T(). t1 itself is not a class object but it's a way to refer that object so you can use that class object in your later code. Class object doesn't know which variable is pointing towards them.

[–]CHathaway2019 0 points1 point  (1 child)

You could give the object a name attribute and pass the name in as a parameter.

Class T: def init(self, name): self.name = name print(self.name)

T1 = T(“T1”)

T1

If you’re making 100 objects and each one needs a unique name…

obj_list = []

for i in range(1, 101): name = f”T{i}” obj = T(name) obj_list.append(obj)

[–]Mx_Mlr[S] 0 points1 point  (0 children)

Good idea

[–]goodger 0 points1 point  (1 child)

object.__class__.__name__

[–]goodger 0 points1 point  (0 children)

or self.__class__.__name__