-4
class Human:

    pass

stack = Human()
stack.name  #<- means? how to hide or kill or make to unusable **.name** ?

Can someone help me? Can I kill such undefined attributes in Object oriented in Python.

Joe
  • 12,057
  • 5
  • 39
  • 55

1 Answers1

0
class Human():
 pass

m1 = Human()
try:
 m1.name
except AttributeError:
 pass # for hiding this error or you can use (print "this attribute does not exist ")

If your intended purpose is deleting an attribute use this methods :

method 1 : use del

  >>> class Human():

    def __init__(self,name,lastname):
        self.name = name
        self.lastname = lastname


>>> m1 = Human('name' , 'lastname')

before deleting

>>> m1.name
'name'
>>> del m1.name

after deleting

>>> m1.name

Traceback (most recent call last):
  File "<pyshell#101>", line 1, in <module>
    m1.name
AttributeError: Human instance has no attribute 'name'

method 2 :
you can use delattr(Human, "name")

Note 1 : method 1 is better than method 2 for see why and more detail click here

keyvan vafaee
  • 464
  • 4
  • 15