Python Knowledge Point Supplements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.