Can't print out the child class variable in method overloading - python

I am a newbie in Python so please bear with me if the question is very simple for you.
Can someone explain why the class variable, name, in Dog class causes an error in the following example? It doesn't make sense to me that d.name is ok to be called, but d.eat() is not ok for method overloading. Thank you very much in advance for your help!
class Animal: # parent class
name = 'Animal'
def eat(self):
print "Animal eating"
class Dog(Animal): # child class
name = 'Dog'
def eat(self):
print name
d = Dog()
print d.name # OK
d.eat() # Error !

Since name is a class member variable, not a global nor local variable, it needs the . operator to look it up. Try one of these:
print self.name
print Dog.name
Which one you use will depend upon other aspects of your program design. The first will attempt to look up name in the current object, falling back to the class definition if required. The second will always use the class definition.

The reason for your error is because you can't define the method with the variable name within that scope. If you do this, then you won't have the error:
class Animal: # parent class
name = 'Animal'
def eat(self):
print "Animal eating"
class Dog(Animal): # child class
name = 'Dog'
def eat(self):
# name does not exist within this scope
print self.name
d = Dog()
print d.name # OK
d.eat() # No longer an error!

Related

How can i add atrributes to the class method from outside class?

This is gonna be my first question on this website. If I do mistake about English, sorry.
Okey, my question is how can I add or set attribute to the class method from outside class?
If i am not wrong,we use settatr() to do this. Can you guys help me pls?
class sss():
def __init__(self,name,surname):
self.name = name
self.surname = surname
def method1(self):
a = "Python"
When we do this:
object = sss("Adam","Roger") #This 2 lines are on outside the class
setattr(object,"Age",19)
What exactly happens? Now this "Age" attribute belong to our object? What is the value of the "Age" attribute now? I mean is that class attribute or what?
My second question is how can I add attribute to the class method?
Thank you
If you instanciate your class with e.g. person = Sss("Adam", "Roger") you can access and add attributes with a dot like this:
person.age = 19
Full example:
class Sss:
# You should name your class with capital letter and without brackets
def __init__(self, name, surname):
self.name = name
self.surname = surname
person = Sss("Adam", "Roger")
print(person.name) # access attribute
person.age = 19 # add attribute
print(person.age)
As #Julian Fock mentioned, you can add attributes to an object by simply writing statements, such as person.age = 19. Your instance/object of the sss-Person class would now have this attribute, but not the class itself.
To modify the class sss-Person itself, you would need to implement some ugly methods, which would be labelled as "hacky" or "bad practices" by most programmers. Companies could refuse to accept the code as valid. You should instead consider using other methods such as class inheritance, where you can derive a modified class adding more advanced functionality.
Still, changing a class definition can be useful for debugging or exceptional emergencies. In that case, you can define external functions, and link them to override your class methods as follows:
# A) Initial Analysis
class Person:
def __init__(self,name,surname):
self.name = name
self.surname = surname
def method1(self):
a = "Python"
person1 = Person("Adam","Roger")
person2 = Person("Adam","Roger")
person1.age = 19
print(person1.age)
# Output: 19
# print(person2.age)
# AttributeError: 'Person' object has no attribute 'age'
# B) Change class attributes
def __init2__(self, name, surname, age):
self.name = name
self.surname = surname
self.age = age
Person.__init__ = __init2__
person3 = Person("Adam","Roger", 20)
print(person3.age)
# Output: 20
# person4 = Person("Adam","Roger")
# TypeError: __init2__() missing 1 required positional argument: 'age'
# C) Overwrite method1
def method2(self):
self.a = "Python"
Person.method1 = method2
person5 = Person("Adam","Roger",44)
person5.method1()
print(person5.a)
# Output: "Python"
What is "Age" attribute now? I mean is that class attribute or what?
Age is now an instance variable on your instance of sss named object.
How can i add attribute to the class method?
I'm not sure what you're asking for here.
PS.: I will not get into the merit of wether it is recommended to do things that I will exemplify here.
Adding to Bill Lynch's answer
What is "Age" attribute now? I mean is that class attribute or what?
Age is now an instance variable on your instance of sss named object.
PS.: Don't use object for it is a keyword in python.
What is the value of the "Age" attribute now?
The value is 19.
I mean is that class attribute or what?
It is a attribute of the instance you created from the class, and only for that instance.
how can I add attribute to the class method?
If you want to add the attribute to the class so that every instance has a new attribute of your choosing, you can do this:
setattr(sss, 'b', 'Python')
obj1 = sss("Adam", "Roger")
print(obj1.b)
obj2 = sss("Adam", "Roger")
print(obj2.b)
obj2.b = "New"
print(obj1.b) # will still be Python
print(obj2.b) # now will be New
If you want to overwrite method1 with a new method:
sss_obj = sss("Adam", "Roger")
def method1(self):
self.b = 'New'
setattr(sss, 'method1', method1)
print('Should be False because the instance has no b attribute yet.', hasattr(sss_obj, 'b'))
sss_obj.method1()
print('Now it has and b value is', sss_obj.b)
If you want to change the method on a specific instance of class sss:
sss_obj = sss("Adam", "Roger")
sss_obj2 = sss("Adam", "Roger")
def method1():
sss_obj2.b = 'New'
setattr(sss_obj2, 'method1', method1)
sss_obj.method1()
print('Should be False because method1 from instance sss_obj does not set b.', hasattr(sss_obj, 'b'))
sss_obj2.method1()
print('Now on instance 2 the b value is', sss_obj2.b)
If you want to change the source of the method as a string programatically:
sss_obj = sss("Adam", "Roger")
new_method_source = 'self.b = "NotRecommended"\n' \
'print("Got into changed method")'
def method1(self):
return exec(
compile(new_method_source, '', mode='exec'),
None,
{
'self': self
}
)
setattr(sss, 'method1', method1)
If you want to change the code from method1, first grab the source with the following line
method1_src = inspect.getsource(sss_obj.method1)
Then change the string as you will and do a similar thing as previously mentioned (compile and stuff).
Cheers

How can I make the OOP code print my classes objects name?

I've tried to make an OOP based program in python. I gave it an object to work with and tried to make it print the name, but its not working.
class human:
def __init__(self, name):
print("this is a human")
def name(self, name):
print("this is {}".format(bob.name))
bob = human("bob")
Anyone know what the problem could be?
Beyond the answers you already received (which solve your problem), I'd suggest not having a method that prints the name. Rather, you should have a __str___ dunder method that defines the object's behavior when an instance is printed.
class human:
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
person = human("bob")
print(person)
'bob'
You can also define the object's behavior when the instance name is entered in the console, for instance just running the line
>>> person
You can do it with __repr__:
def __repr__(self):
return f'when entering the instance name in the console: {self.name}'
This will print:
when entering the instance name in the console: bob
This appears more pythonic to me than having a method that simply prints the name.
You're never storing the name on the instance, where would it get the name from? Your __init__ needs to do something along the lines of self.name = name
the name method and attribute are going to conflict, the latter will shadow (hide) the former, and it should look up whatever attribute its using on self
You never assigned the passed name to the object. Try:
class human:
def __init__(self, name):
print("this is a human")
self.name = name
def print_name(self):
print("this is {}".format(self.name))
bob = human("bob")
bob.print_name()
there are couple of things to update in the code:
bob is an instance which is not defined at human class
notice that init, name functions expect external param but you never use it in the function. (in self. = name)
in order to use it:
define a var in the class named 'name' and update you function to:
class human:
_name = ""
def __init__(self, name):
print("this is a human")
self._name = name
def name(self):
print("this is "+ self._name)
bob = human("bob")
bob.name()
bob = human("bob") only init function and you should call bob.name() in order to call the print-name function

How to call global function from class method

I have the following code:
def __static_func(name):
print 'Name = ' + name
class A:
def __init__(self, name):
self.name = name
def fun(self):
__static_func(self.name)
a = A('foo')
a.fun()
When launched on Python 2.7, it produces
NameError: global name '_A__static_func' is not defined
So the question is how do I call global function from within class method?
I was recently reading a book "Learning Python by O'Reilly" (Page 944, Chapter 31) and it was mentioned that when you use double underscores __ as the starting characters of a method or a variable in the Class, it automatically appends the _classname to that function where classname is the class name. This is done to localize a name to the class to which it belongs. This is called Name Mangling in the context of Pseudoprivate class attributes.
This way you can use the same name __variable in two different classes A and B as the variables/methods will become privately _A__variable and _B__variable respectively. So just name your global function something else with a single underscore for example to avoid this conflict.
Don't use double underscores.
def _static_func(name):
print 'Name = ' + name
class A:
def __init__(self, name):
self.name = name
def fun(self):
_static_func(self.name)
a = A('foo')
a.fun()
Should work

Creating objects within an existing object in Python

I would like to create an object that holds and creates different objects within itself.
I have an outer class and inner classes, like this:
class Outer:
def __init__(self, name):
self.name = name
def sayHello(self):
print "Hello " + self.name
class Inner1:
def __init__(self, name):
self.name = name
class Inner2(Inner1):
pass
class Inner3(Inner1):
pass
new = outer("new")
And then new needs to make on object of inner2 or inner3...
I tried it with new.inner2()
but I donĀ“t get the result I want.
Any tips?
Here is how you would do nested classes and nested instantiations. When you're embedding the classes, you're only embedding the types. You have to create the instances in self.__init__
(If you're trying to do global inner instances shared among all Outer instances please update your question.)
class Outer(object):
class Inner1(object):
pass
class Inner2(Inner1):
pass
class Inner3(Inner2):
pass
def __init__(self):
self.inner1 = Outer.Inner1()
self.inner2 = Outer.Inner2()
self.inner3 = Outer.Inner3()
outer = Outer()
print outer.inner1
print outer.inner2
print outer.inner3
Note that you don't have to actually use nested classes for this -- your classes can be defined outside of your class, and is sometimes preferred as simpler and more Pythonic:
class Inner1(object):
pass
class Inner2(Inner1):
pass
class Inner3(Inner2):
pass
class Outer(object):
def __init__(self):
self.inner1 = Inner1()
self.inner2 = Inner2()
self.inner3 = Inner3()
outer = Outer()
print outer.inner1
print outer.inner2
print outer.inner3
Sometimes you'll also see a pattern of...
class Inner1(object):
pass
class Outer(object):
Inner1 = Inner1
to make a "handy" reference to the class inside the class. This is often used with custom exceptions that the class might throw.
There are many different opinions on whether nesting the classes is preferred.
Honestly inner classes are not generally a good idea, especially if you're instantiating them outside of the "containing" class.
But to answer your question, basically the inner class is just declared in a different scope, so you need to reference the scope it is in.
# Changed to a capitol letter as that is more conventional
class Outer:
name = ""
def __init__(self, name):
self.name = name
def sayHello(self):
print ("Hello" + self.name)
class Inner1:
def __init__(self, name):
self.name = name
class Inner2(Inner1):
pass
class Inner3(Inner1):
pass
newOuter = Outer("newOuter")
newInner2 = Outer.Inner2("newInner2")

__init__ and the setting of class variables

Im having some trouble understanding Inheritance in classes and wondering why this bit of python code is not working, can anyone walk me through what is going wrong here?
## Animal is-a object
class Animal(object):
def __init__(self, name, sound):
self.implimented = False
self.name = name
self.sound = sound
def speak(self):
if self.implimented == True:
print "Sound: ", self.sound
def animal_name(self):
if self.implimented == True:
print "Name: ", self.name
## Dog is-a Animal
class Dog(Animal):
def __init__(self):
self.implimented = True
name = "Dog"
sound = "Woof"
mark = Dog(Animal)
mark.animal_name()
mark.speak()
This is the output through the terminal
Traceback (most recent call last):
File "/private/var/folders/nd/4r8kqczj19j1yk8n59f1pmp80000gn/T/Cleanup At Startup/ex41-376235301.968.py", line 26, in <module>
mark = Dog(Animal)
TypeError: __init__() takes exactly 1 argument (2 given)
logout
I was trying to get animal to check if an animal was implemented, and then if so, get the classes inheriting from animal to set the variables that Animals would then be able to manipulate.
katrielalex answered your question pretty well, but I'd also like to point out that your classes are somewhat poorly - if not incorrectly - coded. There seems to be few misunderstandings about the way you use classes.
First, I would recommend reading the Python docs to get the basic idea: http://docs.python.org/2/tutorial/classes.html
To create a class, you simply do
class Animal:
def __init__(self, name, sound): # class constructor
self.name = name
self.sound = sound
And now you can create name objects by calling a1 = Animal("Leo The Lion", "Rawr") or so.
To inherit a class, you do:
# Define superclass (Animal) already in the class definition
class Dog(Animal):
# Subclasses can take additional parameters, such as age
def __init__(self, age):
# Use super class' (Animal's) __init__ method to initialize name and sound
# You don't define them separately in the Dog section
super(Dog, self).__init__("Dog", "Woof")
# Normally define attributes that don't belong to super class
self.age = age
And now you can create a simple Dog object by saying d1 = Dog(18) and you don't need to use d1 = Dog(Animal), you already told the class that it's superclass is Animal at the first line class Dog(Animal):
To create an instance of a class you do
mark = Dog()
not mark = Dog(Animal).
Don't do this implimented stuff. If you want a class that you can't instantiate (i.e. you have to subclass first), do
import abc
class Animal(object):
__metaclass__ = abc.ABCMeta
def speak(self):
...
Since age in the given example is not part of the parent (or base) class, you have to implement the the function (which in a class is called method) in the class which inheritted (also known as derived class).
class Dog(Animal):
# Subclasses can take additional parameters, such as age
def __init__(self, age):
... # Implementation can be found in reaction before this one
def give_age( self ):
print self.age

Categories