-3

Below is the code snippet that i am executing in sublime text

class Employee:
    def __init__(self, first, last):
        self.first = first
        self.last = last

        @property
        def email(self):
            return '{}.{}@email.com'.format(self.first, self.last)

            @property
            def fullname(self):
                return '{} {}'.format(self.first, self.last)

                emp1 = Employee('Aquib', 'Javed')

                emp1.email()
                emp1.fullname()
                print(emp1.fullname)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149

2 Answers2

0

Your code is not properly indented.

class Employee:
 def __init__(self, first, last):
    self.first = first
    self.last = last

 @property
 def email(self):   #same level indentation as __init__() or it'll have scope under __init__() method only
    return '{}.{}@email.com'.format(self.first, self.last)

 @property
 def fullname(self):
    return '{} {}'.format(self.first, self.last)

emp1 = Employee('Aquib', 'Javed')

emp1.email # Since you've mentioned `email` as property and returning as string, you should just use its property's name
emp1.fullname # same goes here too
print(emp1.fullname)
Madhan Varadhodiyil
  • 2,086
  • 1
  • 14
  • 20
  • can you name some auto indentation tool in sublime text3 – Aquib Javed Jul 29 '18 at 11:46
  • You could give [Python PEP8 Autoformat](https://packagecontrol.io/packages/Python%20PEP8%20Autoformat) a try! (never used it myself though) – Madhan Varadhodiyil Jul 29 '18 at 11:49
  • 2
    Indentation conveys meaning in Python. If such auto-indenting tool existed (and worked), that would mean it could somehow deduce and modify the meaning of your code. I doubt any automatic tool can re-indent your code as in this answer because no tool (as of today) can understand what you _meant_ to write. – ForceBru Jul 29 '18 at 11:51
0

Read about Short Description of the Scoping Rules? (and here about classes: https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes)

Fix your code to:

# your class definition, all that belongs to it, is indented 1 more then "class .... :"
class Employee:
    # method of your class
    def __init__(self, first, last):
        self.first = first
        self.last = last

    # property of your class
    @property
    def email(self):
        return '{}.{}@email.com'.format(self.first, self.last)

    @property
    def fullname(self):
        return '{} {}'.format(self.first, self.last)

# not part of your class, you are creating an Instance of your class
emp1 = Employee('Aquib', 'Javed')

emp1.email      # this returns a string, but you do nothing with it - so it is discarded
emp1.fullname   # this returns a string, but you do nothing with it - so it is discarded
print(emp1.fullname) # this prints it 
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69