0

im learning to make an organizational chart using class

class employee:
    def __init__(self, name , title, salary):
        self.name = name
        self.title = title
        self.salary = salary

abcde = employee("Elon Musk", "CEO",1000000)
print(employee.self) --> error

Basically what im trying to do is to get the "abcde" value, to use it on my other function..

is there anyway to get the name assigned to the object? in this case "abcde"

Recreating this post to clarify what im trying to do:

I simplified the complete code that i have here, but basically what im trying to do here is getting the assigned class name, in this case "abcde".

i understand that self.name will get me "elon musk: self.title will get me "CEO"

but how do i get the "abcde"?

why do i need the abcde? because i need to append the abcde to a list called

direct_report_list= []

to append the "abcde" inside this list, which i will use for this:

def __str__(self):

           otheremp_list = []

           print(self.title,” – “, self.name)

           print(“Direct Reports:”)

           for emp in self.direct_reports_list:

                          print(emp.title,” – “,emp.name)

                          otheremp_list.append(emp.direct_reports_list)*

           print(“other employees:”)

           for emp in otheremp_list:

                          print(emp.title,” – “,emp.name)

                          otheremp_list.append(emp.direct_reports_list)*

i need the "abcde" value so i can add this value into my list, which i use for the "emp" in str

W.tan
  • 23
  • 1
  • 8
  • what do you mean by `employee.self` ??? That doesn't make sense. what's your other function? – eshirvana Feb 13 '22 at 01:51
  • 2
    The object has absolutely no knowledge about what variable name refers to it. Keep in mind that there might be any number of such references, including zero (if the object was an element of a list, for example). – jasonharper Feb 13 '22 at 02:02
  • What are you trying to accomplish? This seems like an [XY problem](https://meta.stackexchange.com/q/66377/343832). If all you want to do is pass `abcde` to a function, you'd write `some_function(abcde)`. On the other hand, if you want to associate a string to the instance, you could use a dict, since [class instances don't have canonical names](/a/18425275/4518341). BTW, welcome to Stack Overflow! Please take the [tour] and read [ask]. You can [edit] to clarify. – wjandrea Feb 13 '22 at 02:06
  • Perhaps also related [How to keep track of class instances?](https://stackoverflow.com/q/12101958/15497888) – Henry Ecker Feb 13 '22 at 02:25
  • Did you read the linked duplicate? How is your question different? – wjandrea Feb 13 '22 at 05:32
  • @wjandrea i deleted that one, and ask for review on this one, like you suggest – W.tan Feb 13 '22 at 05:48
  • @W.tan Sorry, not the one you reposted, I meant [How can I get the name of an object?](/q/1538342) It covers how to get an object's variable name, so how is your question different from that? Sorry, I should have asked you that before suggesting submitting for review. – wjandrea Feb 13 '22 at 05:53

2 Answers2

0

override the __str__method and return a string & to get each value use object + dot + the data which you want to access

class employee:

    def __init__(self, name , title, salary):
        self.name = name
        self.title = title
        self.salary = salary

    def __str__(self):
        return self.name + "-" + self.title + "-"+ str(self.salary)

abcde = employee("Elon Musk", "CEO",1000000)
print(abcde)

#to get each value

print(abcde.name)
print(abcde.title)
print(abcde.salary)

Output:

$ python3 print.py 
Elon Musk-CEO-1000000
Elon Musk
CEO
1000000
Udesh
  • 2,415
  • 2
  • 22
  • 32
0

Looks like you have just started learning classes. I will try to clarify a few aspects that i think may be causing this confusion.

Remember a self keyword for a class is relevant only inside the class to refer to its own objects, and that is why you were able to use self.title in the class

When you create an object abcde you already have the object named abcde; you do not need to refer to it as employee.self.

Hence if you simply want to pass that as an object just pass by what it is actually named i.e. just abcde. Below code may make it more clear!

class employee:
    def __init__(self, name , title, salary):
        self.name = name
        self.title = title
        self.salary = salary

def simplefunc(obj):
    print(obj.name)

abcde = employee("Elon Musk", "CEO",1000000)
#print(employee.self) --> Causes an Error 

print(abcde)
simplefunc(abcde)
ibrez
  • 146
  • 6