-4

I have this little test code to see how classes work.

class Person:
    def __init__(self):
      self.name = ("Mike")

    def myfunc(self):
      print("Hello my name is " + self.name)

p1 = Person()
hello = p1.myfunc()
hello

What I want is for self.name to be returned from the class so I can use it outside of the class. Is this possible? I want the value to be returned without the 'hello my name is' string, so when printing the value returned the output is 'Mike' not 'Hello my name is Mike'. For now, it's just one value as this is a test code. If it works perfectly here, I will put it on my real code to make sure it works.

The Beast
  • 1
  • 7
  • `print(p1.name)`? – Aran-Fey Nov 04 '18 at 09:33
  • You can access the name by `p1.name` - and similarly for any other `Person` object you make from the class – Robin Zigmond Nov 04 '18 at 09:33
  • i already tried putting return in the myfunc(self) but what do I do after that? – The Beast Nov 04 '18 at 09:34
  • 1
    Note that `hello = ...` assigns to `None` because you don't return anything in `myfunc`. – timgeb Nov 04 '18 at 09:34
  • You don't *need* (and shouldn't write) a function that returns a person's name because you can just access it as `p1.name`. – Aran-Fey Nov 04 '18 at 09:35
  • but I want to access the variable and print it out outside the class. – The Beast Nov 04 '18 at 09:36
  • >>> `print(p1.name)` << – Aran-Fey Nov 04 '18 at 09:36
  • so for me to return multiple values, i should just 'return variable1, variable2'? and then split it outside? i will try this now. Also would you mind to post this as an answer if it works? – The Beast Nov 04 '18 at 09:38
  • 1
    How to return multiple values from a function has been answered [here](https://stackoverflow.com/q/9752958/1222951), so unless you explain how your question is different from that one, there's no need to post an answer. – Aran-Fey Nov 04 '18 at 09:40
  • problem: When I go to print, the class always comes up as invalid syntax... EDIT it works, only Mike came up! – The Beast Nov 04 '18 at 09:42
  • Aran-Frey this question is intended for _methods_, not just functions. Therefore the code formatting may be different – The Beast Nov 04 '18 at 09:59

3 Answers3

1

In python there isn't a way to limit or grant access on an attribute. By default, you can access an attribute outside of the object, unlike Java where you can access only attributes that are signed like public. So you can directly retrive the name attribute on your instance by writing person1.name where person1 is an istance of the Person class, however is bad practice in object oriented programming (OOP).

An alternative way is to create two functions, a setter and a getter, that sets and returns respectively value of the interested attribute. In your case it will be:

def setName(self,name):
    self.name = name

def getName(self):
    return self.name

In this way you respect some principles data encapsulation of OOP, and when you need to see what the name simple call the method on the instance person1.getName().

Note:

One advantage of this approach is that now you can verify what value your instance can assume. For Person class and example, setting a number wouldn't make sense so checking if name argument is really a string before updating it will be good.

Read more on private attributes in python and here. For Getters and setter look this link, where you can learn about Properties concept too.

Community
  • 1
  • 1
Iulian
  • 300
  • 2
  • 8
0

Try using p1.pname And p1.myfunc() wont give an output, so there is no point of storing it

0

(1)Return the value(s) from the function in the class as normal, using return variable.

(2)Then to print it outside the class, variable = (myClass().variable) and then print variable.

(3) If you didn't want to print it out, just use the variable as normal.

You have successfully managed to get values from the class and used it in the whole program, without the need for the global keyword!

EDIT it looks like you don't even need to return the values: (myClass().variable) will get the value straight from the class without even needing to return the values, as supported by this code:

class Person:
    def __init__(self):
        self.name1 = ("Mike")
        self.name2 = ("John")
        self.name3 = ("Jane")
        self.name4 = ("Doe")

name1, name2, name3, name4 = (Person().name1), (Person().name2), (Person().name3), (Person().name4)
print (name1, name2, name3, name4)
The Beast
  • 1
  • 7