Python Forum

Full Version: call an instance of a class in the interactive
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello,
I have created a class 'User':
class User:
      def __init__(self, name, gender, age):
            self.name = name
            self.gender = gender
            self.age = age
            
      def __call__(self, name, gender, age):
            print(name)
            print(gender)
            print(age)
      

user1 = User('dany wasser', 'gender: male', 21)
I put this class in a file called saty.py
then I imported the file 'saty.py' in the interactive mode of the interpreter by:
import saty
the file was successfully imported.
now I want to call the single instance of this class 'user1' with all its attributes into the interactive mode of the interpreter. how can I do this?
>>> print ... ?

thanks in advance
If you only want to call the instance, you simply do
>>> user1()
However, being callable is an uncommon feature for a class representing a user. In this case one would more traditionally define a conversion to string with a __str__() method.

class User:
      def __init__(self, name, gender, age):
            self.name = name
            self.gender = gender
            self.age = age
             
      def __str__(self):
            return "User({!r}, {!r}, {!r})".format(
                  self.name, self.gender, self.age)

 
user1 = User('dany wasser', 'gender: male', 21)
Then you could simply do
>>> print(user1)
The method could also be named __repr__() in this case in order to be called if some code asks for repr(user1).
you will usei
saty.user1 to reference name user1 from saty.py after this import.

Now there are other problems with your class. For example you don't need this __call__ function. For this class it doesn't make sense it to be callable. If you want to create nice printable representation of the class, you will use __str__. There is also another special method - __repr__ which you may want to implementit's bit different.

class User:
    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age
             
    def __str__(self):
        return 'User {}, gender: {}, age: {}'.format(self.name, self.gender, self.age)

    def __repr__(self):
        return 'User object: (id: {}, name: {}, gender: {}, age: {})'.format(id(self), self.name, self.gender, self.age)
       
 
user1 = User('dany wasser', 'gender: male', 21)
Output:
>>> import saty >>> saty.user1 User object: (id: 139691134272064, name: dany wasser, gender: gender: male, age: 21) >>> print(saty.user1) User dany wasser, gender: gender: male, age: 21 >>>
(Aug-23-2018, 06:37 AM)Gribouillis Wrote: [ -> ]If you only want to call the instance, you simply do
>>> user1()
However, being callable is an uncommon feature for a class representing a user. In this case one would more traditionally define a conversion to string with a __str__() method.

class User:
      def __init__(self, name, gender, age):
            self.name = name
            self.gender = gender
            self.age = age
             
      def __str__(self):
            return "User({!r}, {!r}, {!r})".format(
                  self.name, self.gender, self.age)

 
user1 = User('dany wasser', 'gender: male', 21)
Then you could simply do
>>> print(user1)
The method could also be named __repr__() in this case in order to be called if some code asks for repr(user1).

it works excellent!
thanks a lot!

(Aug-23-2018, 06:37 AM)buran Wrote: [ -> ]you will usei
saty.user1 to reference name user1 from saty.py after this import.

Now there are other problems with your class. For example you don't need this __call__ function. For this class it doesn't make sense it to be callable. If you want to create nice printable representation of the class, you will use __str__. There is also another special method - __repr__ which you may want to implementit's bit different.

class User:
    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age
             
    def __str__(self):
        return 'User {}, gender: {}, age: {}'.format(self.name, self.gender, self.age)

    def __repr__(self):
        return 'User object: (id: {}, name: {}, gender: {}, age: {})'.format(id(self), self.name, self.gender, self.age)
       
 
user1 = User('dany wasser', 'gender: male', 21)
Output:
>>> import saty >>> saty.user1 User object: (id: 139691134272064, name: dany wasser, gender: gender: male, age: 21) >>> print(saty.user1) User dany wasser, gender: gender: male, age: 21 >>>

very sophisticated version!
thanks a lot!