Content Summary
1.__str__
2.os.path Related methods
1.__str__
Let's first define a Student class and print an instance:
1 class Student (object): 2 def __init__ (self, name): 3 Self.name = name45print Student ('Michael' )
<__main__.Student object at 0x109afb190>
Print out a bunch <__main__.Student object at 0x109afb190> , not pretty.
How can you print it well? Just define a good __str__() method and return a nice-looking string:
class Student (object): def __init__ (self, name): = name def__str__(self): return self.name Print (Student ('Michael'))
Michael
This kind of printed example, not only good-looking, but also easy to see the important data inside the instance.
2.os.path Related methods
#os. Path.dirname () to remove the file name and return to the path where the directory is located
# os.path.join () is used to combine parts of the separation into one pathname
#os. Path.abspath () returns the path normalized absolute path
Import= Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__' ) DB'admin' db ' course')
Python Knowledge Point Supplements