https://github.com/stoicsapien1/DSMP-2.0
class Person:
def __init__(self,name_input,country_input):
self.name = name_input
self.country_input = country_input.lower()
def greet(self):
if self.country_input=="india":
print("Namaste",self.name)
else:
print("Hello!",self.name)
Here we have defined only two attributes name and country_input ,but still we can define new attributes for a particular object.
Python allows you to create new attributes for objects outside the class definition by simply assigning them (like p.gender = "male"
).
These dynamic attributes are specific to that object instance and don't affect the class or other instances.
# object without a refernce
class Person:
def __init__(self):
self.name = "nitish"
self.gender = "male"
p = Person()
q=p
Note: p is just a reference variable it means that it is not the object itself,but it just contains the address to that object.
The id of p and q are the same,it means both contains the address of the same object.
Also,if you will change the attribute using a particular variable,the attribute of another variable will also going to get changed.
Reference variables in Python store memory addresses, not objects themselves.
Key Points: