Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6791723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:52:51+00:00 2026-05-26T17:52:51+00:00

In the code following, OP created a class with inheriting Template class class BatchRename(Template):

  • 0

In the code following, OP created a class with inheriting Template class class BatchRename(Template): and at the bottom of the code, he is instantiating the class as t = BatchRename(fmt) albeit there is no construction method (def __init__():). As I understand inheritance in Python, there might be something wrong with this code but I am new to Python so I might be missing something. Could anyone explain me this?

Little code explaining: The OP shows how to use custom delimiters in Templates and he is doing a Batch Renaming on some files in the example. He is creating a custom class which inherits from Template to change the delimeter name(field,property,attribute) and use the inner functionality at the same time.

>>> import time, os.path
>>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
>>> class BatchRename(Template):
...     delimiter = '%'
>>> fmt = raw_input('Enter rename style (%d-date %n-seqnum %f-format):  ')
Enter rename style (%d-date %n-seqnum %f-format):  Ashley_%n%f

>>> t = BatchRename(fmt)
>>> date = time.strftime('%d%b%y')
>>> for i, filename in enumerate(photofiles):
...     base, ext = os.path.splitext(filename)
...     newname = t.substitute(d=date, n=i, f=ext)
...     print '{0} --> {1}'.format(filename, newname)

img_1074.jpg --> Ashley_0.jpg
img_1076.jpg --> Ashley_1.jpg
img_1077.jpg --> Ashley_2.jpg

I took this code from: http://docs.python.org/tutorial/stdlib2.html#templating

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:52:52+00:00Added an answer on May 26, 2026 at 5:52 pm

    You don’t need an __init__ in every Python class; it’s optional and only called when present. The actual constructor for new-style Python classes is __new__, which is inherited from object:

    >>> class Foo(object):
    ...  pass
    ...
    >>> Foo()
    <__main__.Foo object at 0x021757F0>
    >>> Foo(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: object.__new__() takes no parameters
    

    If a class inherits from a class that does provide __init__, that function is inherited along, just like any other method. This is different from, say, C++, where constructors are treated as being special and are not inherited.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created following code snippet: class PimpToRight[A](f: A => Boolean) { def <| (a:
I have the following code. The html elements with the class .acc_container are created
I've created a class myString and I'm trying to run the following code: class
I have the following code that creates a serverside object of the xmlhttp class.
Following is the code. create a class lib add the ref to NUnit framework
The following code can be found in the NHibernate.Id.GuidCombGenerator class. The algorithm creates sequential
I created the following code to calculate the duration between two timestamps which can
the following code (from a database object created for each new view): -(void)update:(NSString *)queryText{
I created a new project with the following code segment: char* strange = (Strange??);
I have a transparent BufferedImage created with the following code(not relevant how it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.